Recording the output stream in a new window

How can I write an Outputstream in a new browser window?

I currently have the code below. Obviously it opens the stream in the same window. I know I can write an Outputstream to a file and open it in a new window, but that's not an option.

protected void openPDF(byte[] dados) {

   HttpContext contexto = HttpContext.Current;

   contexto.Response.Clear();
   contexto.Response.AppendHeader("content-type", "application/pdf");
   contexto.Response.AppendHeader("Expires","Mon, 26 Jul 1990 05:00:00 GMT");
   contexto.Response.AppendHeader("Cache-Control","no-cache, must-revalidate");
   contexto.Response.AppendHeader("Pragma","no-cache");
   contexto.Response.Expires = -1;
   contexto.Response.AppendHeader("Content-Disposition", "inline; filename=labels.pdf");
   contexto.Response.AddHeader("content-length", dados.Length.ToString());
   contexto.Response.OutputStream.Write(dados, 0, dados.Length);
   contexto.Response.OutputStream.Flush();
   contexto.Response.End();
  }

      

+2


source to share


4 answers


The link requesting the file should indicate that you want a new browser window.

<a href="/path/to/file" target="_blank">PDF</a>

      



Once a request is sent to the server, the server simply replies with the result, placing it in the requested window, unless you specify a different target. Obviously changing it in your application will require downloading, but you know this and want to avoid it.

Other solutions would be to write an answer that uses some javascript to open a new window and make an actual data fetch request. If you know you always want it in a new window, I think changing the link to point to a new window is the best solution.

+1


source


The server cannot do it alone ...

The server only responds to requests and the client displays / uses these responses as it sees fit.

You will need to change the client side application flow by asking the client to request this PDF (or other content) in a new browser window. This can be easily done, for example, if the PDF is currently being submitted after the user clicks on the link, add a target = 'some_window_name' attribute to the element.



In other words, by the time the server receives the request, it is too late (*) to change the "destination" for the response. So the idea is for the client to make a request from / for a new browser window.

(*) too late ...
Well ... maybe not, someone can find a clever way to wrap the response with some javascript envelope that will open a new browser window and somehow (?) run its content from PDF content. Or maybe some other trick ... Anyway, this will probably be somewhat contrived ... (compared to the browser asking for it in the correct context)

+1


source


Is the file generated as part of the postback? If it's that hard. your best option might be to change Content-Disposition

to attachment

, which prompts the user to open the Open / Save dialog rather than display the file. If not, using target="_blank"

in reference is the solution.

0


source


where you created file you can use this code to open local file in new window

Process process = new Process();
process.StartInfo.UseShellExecute = true;
process.StartInfo.FileName = outputPdfFile;
process.Start();

      

-1


source







All Articles