ASP.NET 4: HttpResponse open in NEW Browser? - asp.net

I am using a PDF generater that utlizes HttpResponse. Is there a way (perhaps passing a header tag) to open the PDF in a NEW windows instead of the same one? I don't want the user to be directed away from the website...
Here's the code I'm using:
System.Web.HttpResponse response = System.Web.HttpContext.Current.Response; response.Clear();
response.AddHeader("Content-Type", "application/pdf");
response.AddHeader("Content-Disposition",
"inline; filename=" + downloadName + "; size=" + downloadBytes.Length.ToString());
response.Flush();
response.BinaryWrite(downloadBytes);
response.Flush();
response.End();

You cannot do that on server side if you have not generated the link yourself. But if you have, then as Robert said, provide a target with the <a> link.
There is a server-side alternative and that is to set the content type to application/octect-stream so that the file is download and user will be able to open it with the application of choice outside browser. See here for more.
You also need to use content disposition header to provide the file name so that client can know what file type is it after it has been downloaded as binary.
Content-Disposition: attachment; filename=my.pdf;

You presumably have a link on your site that directs the user to the URL where this PDF generation is done. In that link, add target="_blank" to the <a> tag.

If what you're really asking is how to make the save dialog to show instead of having the PDF loaded into your browser, then you need to modify your header.
You are setting Content-Disposition to 'inline', change that to 'attachment'
http://support.microsoft.com/kb/260519

Related

How to set Browser when using Response.Write

My application is writing XML to a file
which is then downloaded (Response.Write) and the user can then open.
Question:
No matter what Browser I'm using (Chrome, Firefox,etc.) the downloaded file is
opened in IE.
I want the downloaded file to be opened in the browser which created it.
strXML = "<ROWSET></ROWSET>";
Response.Clear();
Response.Charset = "";
Response.ContentType = "text/xml";
Response.AddHeader("content-disposition", "attachment; filename=\"kupot.xml\"");
Response.Write(strXML);
Response.Flush();
Response.End();
Response.Close();
What should I do so that if the downloaded file was created in, for example, Chrome, then when it's downloaded and I'm asked if I want to open it, then if I answer YES then it'll be opened in Chrome
All help/ideas is appreciated.
Thanks
David
Drop the content disposition header. This tells the browser you want to download it as a raw file. This of course will be opened by whatever is associated with the XML file type on your system.
If you just want to return XML content directly to the browser use:
Response.ContentType = "text/xml";
Response.Write(strXML);
Response.Flush();
You also probably don't need to do Clear, Charset, End and Close.

download xml file using asp.net

I have created a xml file using c# and I can save it at any location in my machine (in this situation I have saved it on the root of the application with name "temp.xml"), but I want to let it for the user to download it from their browser buy giving a link like-->
"click HERE to download the file."
In Chrome and FireFox it show a new tab with only some values in my body part of the xml file but IE shows the whole xml. I want to download it in my download folder when anybody click on the above link.
thanks in advance for your support.
You need to specifically mention the file type and name. and use the TransmitFile method. This will show the Save As window.
I got the following code from this webpage
Response.ContentType = "image/jpeg";
Response.AppendHeader("Content-Disposition","attachment; filename=SailBig.jpg");
Response.TransmitFile( Server.MapPath("~/images/sailbig.jpg") );
HttpContext.Current.ApplicationInstance.CompleteRequest();
Response.AddHeader("content-disposition", "attachment;filename=temp.xml");
Response.ContentType = "text/xml";
Response.Write(File.ReadAllText(Server.MapPath("~/temp.xml"))); //you may want to write the XML directly to output stream instead of creating an XML file first.
Response.End();
Hope this helps.

Pop up to save image in asp.net

I am using asp.net 3.5 and C#.
I have a image which I want user can download.
Like, there would be a download button or link. When user click on this link he will be prompted with a pop up to save that image to his desktop.
I have tried with
<a href ="path" > </a>
but it is opening the image in other page, I want user to be prompted to either save or view the image,
please help
Thanks in advance
You need to write an IHttpHandler that serves the image along with a Content-Disposition header.
For example:
Response.AppendHeader("Content-Disposition", "attachment; filename=\"MyImage.png\"");
Response.TransmitFile(path);
You would probably pass the image name on the query-string.
If so, make sure it doesn't contain / or \, or attackers will be able to read arbitrary files.
You need to have another page or, better yet, an HttpHandler, that takes the image path as part of the query string or as a post parameter that will send the response with Content-Disposition set to attachment. Setting the content disposition this way will cause the browser to display the file download dialog. A slightly easier way, though it depends on the user doing something extra is simply to have the link open the image in a new page and let the user right-click on it and do a "Save As".
Download
or
<a href="/path/to/image" target="_blank">
Load Image in New Window then Use Save As</a>

Redirecting to EXEs with Query String Params and IE6/7

Greetings!
I'm scratching my head, wondering why when I do the following:
Response.Redirect(#"http://www.example.com/file.exe?id=12345");
Both IE6 and IE7 will download the file as "file" (with no extension), but Firefox, Opera, Google Chrome and Safari have no problems at all downloading the file as "file.exe".
Any idea what IE6/7's problem is and how can I fix this?
Have You tried to set correct content-type in Your response headers? For example:
Content-Type: application/octet-stream
Content-Disposition: attachment; filename="file.exe"
You will probably need to get the filesize remotely and add it to Content-Length on the header section.
If you use fiddler2 (http://www.fiddler2.com/fiddler2/) you can see exactly what headers are getting sent to IE which may help you in debugging.
Perhaps you can post the resulting headers here?
I doubt that adding the Content-Type and Content-Disposition prior to the redirect will have any affect, since the browser sees the redirect header and makes a completely new http request to the redirected url, which will be an entirely different set of headers.
However, you might try a Server.Transfer which is a server side redirect, something like the following:
Response.Clear(); //In case your .aspx page has already written some html content
Response.AddHeader("Content-Type", "application/octet-stream");
string disp = String.Format("attachment; filename={0}", fileName); // fileName = file.exe
Response.AddHeader("Content-Disposition", disp);
Server.Transfer(#"http://www.example.com/file.exe?id=12345");
Or alternatively, use Response.BinaryWrite :
Response.Clear(); //In case your .aspx page has already written some html content
byte[] exeContent = ... //read the content of the .exe into a byte[]
Response.AddHeader("Content-Type", "application/octet-stream");
string disp = String.Format("attachment; filename={0}", fileName); // fileName = file.exe
Response.AddHeader("Content-Disposition", disp);
Response.BinaryWrite(exeContent);

File download does not work with IE7

I have an asp.net page which sends content of a file to the client, so the browser shows the save as dialog to download the file. This page is displayed in a popup and when the user clicks the save button, it closes automatically and the download starts.
On windows server 2003, it works fine. On vista with other browsers, also works fine. But when I try with IE7 & Vista, the popup opens, and closes after about a second without displaying the file download dialog. How can I solve this?
The code I use for response generation is:
FileStream fileStream = new FileStream(filePath, FileMode.Open);
int fileSize = (int)fileStream.Length;
byte[] buffer = new byte[fileSize];
fileStream.Read(buffer, 0, (int)fileSize);
fileStream.Close();
Response.Clear();
Response.Buffer = true;
Response.BufferOutput = true;
Response.ContentType = "application / octet - stream";
Response.AddHeader("Content-Length", buffer.Length.ToString());
Response.AddHeader("Content-Disposition", "attachment; filename=" + filename);
Response.AddHeader("Extension", Path.GetExtension(filename));
Response.ContentEncoding = System.Text.Encoding.GetEncoding("windows-1254");
Response.BinaryWrite(buffer);
Response.Flush();
Response.End();
And I am opening the popup with this javascript:
window.open ('Download.aspx?filename=somefile.ext','downloadWindow','location=0,status=0,scrollbars=0,width=1,height=1');
EDIT: I corrected the spaces but unfortunately they are not the problem.
EDIT 2:: Seems that this problem is not related to Vista but IE only. I also discovered that it works fine when the project is run on the development server locally but when working as connected to publish server, it fails to download the file.
Try removing the spaces in your ContentType. The standard is application/octet-stream.
Two things.
As mentioned before you will want to remove the spaces in the type
Is there any particular reason that you are not using Response.TransmitFile() rather than reading the file in yourself?
I'd also suggest you add quotes around the file name, otherwise, if it contains spaces, it will get truncated in Firefox.
I can't point to a specific problem in your code (except possibly for that content type, which looks badly-formed; not sure if that makes a difference). Here's the code I use for this, which works in both IE7 and Firefox:
Response.ContentType = "application/x-download";
Response.AddHeader("Content-Disposition", "attachment; filename=" + fileName);
Response.CacheControl = "public";
Response.OutputStream.Write(byteArr, 0, byteArr.Length);
Response.End();
I think your problem could be with IIS 7 . There is a problem with "addHeader" in the new Internet information Server with the integration pipeline mode.
Try to use Response.AppendHeader .
I also had the same problem...and I used this solution (I'm using it on a button.click):
Response.ContentType = "text/txt";
Response.AppendHeader("Content-Disposition", "attachment; filename="+DownloadFileName);
Response.Write(MyFileContent_Text_);
Response.End();
...it just worked!!
I came across this post because I was having a similar problem if not the same one. I am running IE8 on Windows 7.
When debugging on my local machine I could get the File Download prompt to display, but when clicking "Save" or "Open" the Download Progress window would display for about a half second and then close suddenly without downloading anything.
I have an add-on installed for Internet Explorer called IE7Pro. It comes with a Download Manager which I had enabled. When I disabled it, my problems went away and I could Open or Save my files.
Hope this proves helpful to someone else out there.

Resources