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.
Related
I have some asp.net application, which contains 3 rdlc reports. I open those reports in report viewer and it works great. I can download report as pdf file, and also print it from report viewer. Now what I would like to do is this (only if using mozzilla browser): I want to show only pdf file of the report in browser, actually in mozzila pdf viewer. here is the link as an example: http://www.selab.isti.cnr.it/ws-mate/example.pdf (open it in mozzila).
And also i would like to know if this could be done with javascript. Just to mention, i open this reports on button click.
Hope I was clear enough with my question, if you need some more info let me know.
Not javascript but here's how I would do it. For simplicity I'm assuming the report is already loaded into the reportviewer.
In your button click event handler:
//perform check for mozilla... see http://msdn.microsoft.com/library/3yekbd5b
if (browser.Name == mozilla)
{
//get report from viewer
byte[] bytes = ReportViewer1.LocalReport.Render("PDF");
//display pdf in browser
Response.AddHeader("Content-Disposition", "inline; filename=MyReport.pdf");
Response.ContentType = "application/pdf";
Response.BinaryWrite(bytes);
Response.End();
}
Let me know if I missed something but I think understood what you wanted.
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.
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
After submitting a form, the user is presented with a link to a pdf document.
The link is straight to the document, it is not streamed.
If the user right-clicks and chooses 'save link as,' the document saves and opens fine. However, if the user just clicks on the link, the browser takes a very long time to respond (I'm going to guess it's 3 minutes) and then adobe reader gives the following error:
"the file is damaged and could not be repaired"
This is in Chrome v5, ASP.NET 3.5 and the link is returned inside an UpdatePanel.
it depends on browser settings that are configured for PDF Links. If you change the settings in broweser, you will get the download dialog.
Mozilla Firefox
Open Mozilla Firefox
Click Tools and then Options
Within the Options window click Applications
Select the Content Type you wish to adjust. For example, if you want to change how a .PDF file opens in Firefox, select Adobe Acrobat Document.
In the Action section, change the action to how you wish to open the file. If you want to download .PDF files instead of opening them, select Save file.
Internet Explorer:
You have to right click and click save target as to download.
The other option is, create a seperate asp.net and write below code to download the PDF
private void Page_Load(object sender, System.EventArgs e)
{
//Set the appropriate ContentType.
Response.ContentType = "Application/pdf";
//Get the physical path to the file.
string FilePath = MapPath("acrobat.pdf");
//Write the file directly to the HTTP content output stream.
Response.WriteFile(FilePath);
Response.End();
}
I've had this issue before and the cause was the PDF itself.
Adobe has a slew of causes for this: http://kb2.adobe.com/cps/328/328233.html
FWIW, my PDf issue was solved by opening the PDF in Adobe and going to Document -> Reduce File Size -> Make Compatible with Version 7. (current version - 2)
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.