Download an Excel file

I have read some past posts here on how to download a Excel file from a website. So, I have setup the below code:

string path = MapPath(fname);
string name = Path.GetFileName(path);
string ext = Path.GetExtension(path);
string type = "application/vnd.ms-excel";


if (forceDownload)
{
    Response.AppendHeader("content-disposition",
        "attachment; filename=" + name);
}

if (type != "")
{
    Response.ContentType = type;
    Response.WriteFile(path);
    Response.End();
}

However, I get no download dialog box.

I try this both in IE 8 and FireFox 10.0.2.
The file is there, it’s not locked, and it’s not set to read only.
I’m not sure were I went wrong.

Answers:

Thank you for visiting the Q&A section on Magenaut. Please note that all the answers may not help you solve the issue immediately. So please treat them as advisements. If you found the post helpful (or not), leave a comment & I’ll get back to you as soon as possible.

Method 1

According to this link, you need to add this line:

strFileName = Path.GetFileName(path);
Response.TransmitFile( Server.MapPath(strFileName) );

This will cause a Open / Save As dialog box to pop up with the filename of SailBig.jpg as the default filename preset.

This of course assumes you’re feeding a file that already exists. If you need to feed dynamically generated – say an image [or any file] that was generated in memory – you can use Response.BinaryWrite() to stream a byte array or write the output directly in Response.OutputStream.

EDIT:

Microsoft’s MSDN site has a detailed explanation about File Downloading. It includes both samples for Java and .Net applications, the concept is the same:

  1. Get the response.
  2. With the response:
    1. Set the content type to “APPLICATION/OCTET-STREAM” (it means there’s no application to open the file).
    2. Set the header to “Content-Disposition”, “attachment; filename=”” + + “””.
    3. Write the file content into the response.
  3. Close the response.

So, looking at the MSDN ASP.Net file download, you’re lacking the 2.3 step. You’re just writing the file name to the response.

// transfer the file byte-by-byte to the response object
System.IO.FileInfo fileToDownload = new
    System.IO.FileInfo("C:\downloadJSP\DownloadConv\myFile.txt");
Response.Flush();
Response.WriteFile(fileToDownload.FullName);

With this example you will download your file successfully, of course if you can get the file with no problems :).

EDIT 2:

The HTML component used to download any file must be a regular HTML Request. Any ajax request to download a file won’t work. Microsoft explains that here. And the main quote:

Its impossible to attach an event before and after a download through javascript. Browser doesn’t allow this type of events for security reasons.

Method 2

You need to send this before the file attachment header:

Response.ContentType = "application/vnd.ms-excel"

See: Export data to excel file from Classic ASP failing

Method 3

Try adding such HTTP headers

Content-Type: application/force-download
Content-Type: application/vnd.ms-excel
Content-Type: application/download


All methods was sourced from stackoverflow.com or stackexchange.com, is licensed under cc by-sa 2.5, cc by-sa 3.0 and cc by-sa 4.0

0 0 votes
Article Rating
Subscribe
Notify of
guest

0 Comments
Oldest
Newest Most Voted
Inline Feedbacks
View all comments
0
Would love your thoughts, please comment.x
()
x