i'm trying to write code that download a file that located on the server.
but the save as dialog wont open in IE.
i tried response.redirect, i tried
Response.Clear();
Response.ContentType = "text/csv";
Response.AddHeader("Content-Disposition", "attachment; filename=" + fileNameDisplay);
Response.WriteFile(Server.MapPath(pathName + fileNameUnique));
Response.Flush();
Response.End();
every thing works on firefox and chrome , but not in internet explorer.
i know that there is a security option for this in security ---> custom level ---> Downloads ---> automatic prompting for file downloads , that is always in disable mode and i need to switch it to enable in order it to work, but i don't wont that my users deal with this.
how do i overcome this 'security problem' ?
is there any right way to deal with download files ?
what is the right code to do that ?
thank you,
gadym
var info = new FileInfo(path);
Response.Clear();
Response.AppendHeader("Content-Disposition", String.Concat("attachment; filename=", info.Name));
Response.AppendHeader("Content-Length", info.Length.ToString(System.Globalization.CultureInfo.InvariantCulture));
Response.AppendHeader("Content-Transfer-Encoding", "binary");
Response.ContentType = "text/csv";
Response.WriteFile(info.FullName, true);
Response.End();
You need explicitly tell that it is not opened content,
add following headers:
Response.AppendHeader("Content-Disposition", "attachment; filename=" + fname);
Response.AppendHeader("Content-Transfer-Encoding", "binary");
Also it is strongly recommend to set explicit file length
Response.AppendHeader("Content-Length", responseContentLength.ToString());
abatishchev, thank you for helping me.
i found a soultion to my problem.
I created a dialog window (let's call it 'DownloadWindow') which holds an empty 'A HREF' tag.
(it's shows - 'click here To Download')
after i click a download (EXCEL/CSV icon) button on my default page (which creates my file
dynamicly),
i load the dialog window ('DownloadWindow') and then i'm repleacing the 'a href' link to the file url i created earlier so my users can download it from my server.
Now, Internet explorer did pop up the open/save/cancel dialog box.
it's a bit annoying but it's solved my porblem.
I had the same problem when my link for file download was made using:
<a href="DownloadFile.aspx">
<input type="image" src="~/virtual/path/to/image.png" runat="server" />
</a>
The problem was gone when i changed it to:
<a href="DownloadFile.aspx">
<img src="~/virtual/path/to/image.png" runat="server" />
</a>
Related
I would like to give my users an option to select a directory path on their local machine (not select an actual file).
This is so they can save xml files into a directory on their hard drive. When user selects the directory I want to pass that path to my code which then loads data from the xml files into my database.
I know I can get the users to select a file:
<label for="file">Filename:</label>
<input type="file" name="file" id="file" />
But this is not what I want, I want them to just select the directory.
Can someone advise if this is easily done in an MVC application.
A web application cannot directly write to a user's computer. The best you can do is provide a downloadable link and the user needs to do a SAVE AS from the browser.
In your case, forget about the directory path and just give a link/button on your web page to get the XML files.
On click, generate the XML file from the Database and use the following snippet for the user to get a download prompt.
// code on the click of the button/link
FileInfo file = new FileInfo(tempFilePathOnServer);
Response.Clear();
Response.ClearHeaders();
Response.ClearContent();
Response.AddHeader("Content-Disposition", "attachment; filename=\"" + file.Name + "\"");
Response.AddHeader("Content-Length", file.Length.ToString());
Response.ContentType = "text/xml";
Response.Flush();
Response.TransmitFile(file.FullName);
Response.End();
I am trying to open a file in the browser using BinaryWriter. This causes the a dialog window to open and prompt the user to save or open the file. That behavior is fine; however, if I select open, the aspx page is called again and after a long wait the file finally opens.
I set the ContentType
Response.BinaryWrite(binary);
Response.End();
Repsonse.Close();
This behavior only occurs with excel and word files.
Browser IE8
A substitute way of doing the same. You can check it out:
FileInfo fileInfo = new FileInfo(yourFilePath);
context.Response.Clear();
context.Response.ContentType = contentType; //Set the contentType
context.Response.AddHeader("content-disposition", "attachment; filename=" + Path.GetFilename(yourFilePath));
context.Response.AddHeader("Content-Length", fileInfo.Length.ToString());
context.Response.TransmitFile(yourFilePath);
Hope this will help
I have a gridview in which I have provided an option for the user to download the pdf files. When they click on the pdf icon sometimes it open the pdf file in a new tab and sometimes it starts downloading. How can i make it download always?
You need to add a button (image button, linknbutton or button) and handle the RowCommand event of GridView. In RowCommand handler you may write code to download a file.
You may use Response object's method.
string filepath=MapPath("~/files/file.pdf");
byte []bytes=System.IO.File.ReadAllBytes(filepath);
Response.Clear();
Response.ClearHeaders();
Response.AddHeader("Content-Type", "application/octet-stream");
Response.AddHeader("Content-Length", bytes.Length.ToString());
Response.AddHeader("Content-Disposition","attachment; filename=file.pdf");
Response.BinaryWrite(bytes);
Response.Flush();
Response.End();
In order to always force a download you need to add the Content-Disposition header as AVD showed; however, I find this totally unnecessary; I think it would suffice to have the link to the PDF open in a new window. In other words, have target="_blank" defined. Example:
invoice
Then, is up to the user whether he wants to save the file locally or just see it on the screen. I think the important thing is that this won't interfere with the current page the user is looking at.
What's the best way to allow a user to pull down an RDP file, but to do so using AJAX? In other words, I have a hyperlink and I need an RDP file to be downloaded by the user, but without a full page refresh.
I tried to make an AJAX call using the following example for RDP. It seems to work on Chrome and Firefox, but not on any version of IE.
String content = <RDP Content Here>
Response.Clear();
Response.AddHeader("Content-Disposition", "attachment; filename=HelloWorld.rdp");
Response.Cache.SetCacheability(System.Web.HttpCacheability.NoCache);
Response.ContentType = "Content-Type=application/x-rdp rdp;charset=ISO-8859-1";
Response.AddHeader("Content-Length", content.Length.ToString());
Response.Write(content);
Response.End();
I'd create another page and put that code there. Then have that page open up in a new window when the link is clicked. Make you clear out any of the boiler plate code that VS automatically generates when you create a new file. That'll get you your data without a page refresh. I've done this with Excel pages and images and it's worked like a champ.
How can i show a Save as window in asp.net and C#...
This is similar to the one that opens for uploading but that window is the open file...
I am not looking to save only one file...like
Response.AddHeader("Content-Disposition", "attachment; filename=" + myfile.Name);
can i make it ung ajax... please help... samples or suggestions..
thanks
As far as I know you don't have any way of interacting with the file system using javascript from inside the browser. I would imagine that this would be a fairly large security hole if you could. The best I can suggest is to package up your files in a zip file then send that in a response with the content disposition set to attachment, then let the browser handle it for you.
In your page:
<iframe src="getFile.aspx?id=1"/>
<iframe src="getFile.aspx?id=2/>
Then inside getFile.aspx
Response.AddHeader("Content-Disposition", "attachment;filename=" + filename);
Response.BinaryWrite(fileContents);
(Note: never tried this myself...)