In my ASP.NET web application, downloading an output Excel file is sometimes causing an error. In such cases, the file is not downloaded correctly, but instead it's downloaded with XML data, or I get an error:
Could not download the file error
Clicking "retry" enough time makes it work in IE browser.
Note: the output file is downloaded from IIS server where the application was hosted. i.e. C:\inetpub\wwwroot\MyWebApplication\OutputFiles
I am using the following code to download the file:
Response.Clear();
Response.ClearHeaders();
Response.ClearContent();
Response.ContentType = "Application/txt";
string strFileName = (((ASPxButton)sender).CommandArgument).ToString();
string FilePath = MapPath("~/OutputFiles/" + jobNumber + "/" + strFileName);
Response.AddHeader("Content-Disposition", "attachment; filename=" + strFileName);
Response.TransmitFile(FilePath);
Response.Flush();
System.Web.HttpContext.Current.ApplicationInstance.CompleteRequest();
Response.Close();
The above code is working fine but I don't know why there is an error some times. How to solve this issue? Please advice me.
UPDATE: "The file could not download" error is occurred by following scenario,
Two users, download the output files from different path in parallel. I.e (In IE browser) user1 click the download & save button, at the same time user2 click the download & save button (both user download the different files from different path in IIS directory). which means, One user try to download a file while another file is already downloading by another user.
How can fix this issue while parallel download?
Thanks,
Kavin.S
In your system.web node of your web.config alter or add the following where xxxx is the appropriate file size in KB.
<httpRuntime maxRequestLength="xxxx">
https://msdn.microsoft.com/en-us/library/e1f13641(v=vs.71).aspx
Currently my steps is opening a new page with all the details inside the aspx and in the code behind is coded below.
Response.ClearContent();
Response.ClearHeaders();
Response.ContentType = "application/vnd.ms-excel";
Response.AddHeader("content-disposition", "attachment; filename=TenatReport.xls");
So the page will be converted to excel file and system will prompt to save the excel file to their local folder. This is currently working fine and no problem at all.
Now I need help and advise on how to save the excel file to the server instead of saving in local folder without the prompting message. Is there a way using the http response output stream to save the page to the server side or perhaps output stream to save in the database as binary then I will retrieve from the database as binary.
Please note I am still using 1.1 framework.
I tried this code below but it logs out the url after calling the wc.
string fileName = string.Format("{0}{1}", System.IO.Path.GetTempPath(), _TenantID.ToString() + ".xls");
System.Net.WebClient wc = new System.Net.WebClient();<br>
wc.DownloadFile("url:web/Alltenantsrep.aspx?dtmonth=1&dtyear=2014", fileName);
Kindly help as I am stuck for a week. thank you in advance.
I am uploading my files to a sftp server, which users should be able to download later. Now how they are going to download is, I am showing the files in a grid, with their Names (saved in DB) as LinkButtons.
On the linkbuttons click I am going to download the file from sftp server to a local folder on the server and then, here I directly want to give the file to the user. The link button doesnt have a hard link.
Also, there is no restriction of filetype or size. Please can you suggest anything that could help?
To solve the second problem (sending the locally downloaded file), you basically send the file back to the client as the response to the click event:
From How to send a file to a client so a Download dialog will open?:
string pdfPath = MapPath("mypdf.pdf");
Response.ContentType = "Application/pdf";
Response.AppendHeader("content-disposition",
"attachment; filename=" + pdfPath );
Response.TransmitFile(pdfPath);
Response.End();
I'm looking for a way to (easily, by preference ;)) create a download link to a file on a separate file server.
The situation is as follows: the application I'm developing (asp.net 2.0 in vb.net but I have a similar issue in c#, either solution works for me) will be run internally for a company.
As is good practice, the file storage and web application are on two separate servers.
I basically need to be able to create a download link to a file, the only available URL i have to access the file is \servername\folder1\folder2\folder3\file.txt (can be any sort of file)
Weblinks simply don't work. This is how it's currently set up:
tablerowfield.Text = String.Format(
"<a href=""\\servername\folder1\folder2\folder3\{0}""
target=""_blank"">Click me</a>",
filename)
Which doesn't work for obvious reasons. It used to be set up to write that file to the application path itself and that worked perfectly, but it isn't good practice and that's why I'm changing it (or trying to).
I read solutions about creating a download page and then having a table in your DB which holds the links and returns the proper web URL for download but the time constraint I am faced with unfortunately doesn't allow me to develop that.
Assuming I can provide a string with the full filepath to the file like the above, what is the easiest way to just create a link that, when clicked, downloads the document?
Note: I have 0 admin rights in this environment. That really isn't helping me. Let's assume I am given the correct link like above and have the appropriate file access rights and such.
UPDATE:
The above example does work in IE, but not in Firefox and Chrome. IE converts it to a file://servername/... link which does what it's supposed to, but FF and Chrome both actively decided that this is unsafe and have disabled it from their browsers.
You can use ASHX file (say, downloadfile.ashx) and use the following code (not tested, but it will be something like that) in it:
Response.Clear();
Response.ContentType = "application/octet-stream";
Response.AddHeader("Content-Disposition", "attachment; filename=abc.txt");
Response.WriteFile(Server.MapPath("\\servername\folder1\folder2\folder3\abc.txt"));
Response.End();
and then use this in your anchor tag like:
Click me
Note: You can also pass parameters for downloading different files like:
Click me
and then, in ashx file, use the file name to download the appropriate file.
this piece of code will create a file in download folder with name=hi.txt and content as "thanks god, finally file got downloaded."
Response.Clear();
Response.ContentType = "application/octet-stream";
Response.AddHeader("Content-Disposition", "attachment; filename=hi.txt");
Response.Write("thanks god, finally file got downloaded.");
Response.End();
If your file is already there on server then you can use this code in your download button click event like this
protected void downloadpdf_Click(object sender, EventArgs e)
{
Response.Clear();
Response.ContentType = "application/octet-stream";
Response.AddHeader("Content-Disposition", "attachment; filename=""downloadName.pdf""");
Response.WriteFile(Server.MapPath(#"~/path of pdf/actualfile.pdf"));
Response.End();
}
\\servername\folder1\folder2\folder3\... is an UNC path which cannot be used from a browser. Because the file(s) are on a separate server, you need an href attribute of the form http://server-name/folder1/folder2/file.txt.
If the server-name is unresolvable by the clients, then you need to first get the IP address of the server and then formulate the href of the form: http://10.1.1.30/folder1/folder2/file.txt
Here is how you get the IP address from server-name:
IPAddress[] host;
host = Dns.GetHostAddresses("server-name");
string ip = host[0].ToString();
EDIT:
I basically need to be able to create a download link to a file
With the ashx solution your application would be reading the file from the server and relaying it to the clients rather than just providing the clients a link to download the file directly from the file server.
I have an application that uses the regular asp.net upload control to upload files to the server. A user has uploaded a .pptm file (macro enabled powerpoint) and when other users download this file it is corrupt and can't be opened or can be opened but Powerpoints asks the user to "repair" the file and then says some content might have been removed from the file. When the user e-mails me the file I can open it without a problem.
To upload the file I make use of the built in SaveAs method of the FileUpload control.
This is a mimic of how I serve the file to prompt a download dialog to the user:
File ttFile = FileProvider.Get(fileId);
string virtualPath = ttFile.Path;
string fileName = ttFile.FileName;
Response.Clear();
Response.AddHeader("content-disposition", "attachment; filename=\"" + fileName + "\"");
Response.WriteFile(virtualPath);
Response.ContentType = "";
Response.End();
If anynone has any knowledge on why the file would be corrupted I'd love to hear it.
Thanks in advance.