views:

815

answers:

3

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.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();
  }
+1  A: 

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.

tvanfosson
A: 

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)

mjv
A: 

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.

batwad
Basically, I push a button on a Sharepoint Webpart. The webpart calls a webservice returns an byte[]. What I want is to write the byte[] (it's a pdf file) to a new window, since opening it in the same window it's no very user-friendly(ish).
marcocampos
You need to change the button in the webpart to invoke some Javascript to open a new window. The new window will need to be a new page request and you'll have to pass any contextual data needed to call the web service in the querystring.<input type="button" value="Click me" onclick="window.open('somepage.aspx?param=value'" />
batwad