开发者

Write Outputstream in new window

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

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

protected void openPDF(byte[] dados) {

   HttpContext contexto = HttpContext.Current;

   contexto.Respon开发者_JAVA百科se.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();
  }


The link that requests the file needs to specify that you want a new browser window.

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

Once the request is sent to the server, the server simply responds with the result putting it in the window that requested it, unless you've specified a different target. Obviously changing it to an attachment would prompt for download, but you know that and want to avoid it.

Other solutions would be to write a response that uses some javascript to open a new window and perform the actual request to get the data. If you know you always want it in a new window, I think changing the link to specify a new window is a better solution.


The server cannot do that alone...

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

You would need to alter the flow of the application, on the client side, by having the client request this PDF file (or other content), in a new browser window. This can be simply done, for example if the PDF is currently sent following the user clicking on a link, by adding a target='some_window_name' attribute in the a element.

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

(*) too late...
Well... maybe not, someone may find a clever way of wrapping the response with some javascript enveloppe which would open a new browser window and somehow (?) prime its content with the PDF content. Or maybe some other trick... At any rate this would likely be somewhat contrived... (compared with just having the browser ask for it in the right context)


Is the file being generated as part of a postback? If so it's tricky; your best option might be to change Content-Disposition to attachment which will prompt the user with the Open/Save dialog instead of displaying the file. If not, using target="_blank" in the link is the solution.


where you have generate a file,you can use this code open a local file in a new window

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

上一篇:

下一篇:

精彩评论

暂无评论...
验证码 换一张
取 消

最新问答

问答排行榜