Download an Excel file

Source: Internet
Author: User
Aspx: <% @ Page Language = "C #" AutoEventWireup = "true" CodeFile = "test. aspx. cs" Inherits = "test" %>

Test. aspx. cs: Protected void Page_Load (object sender, EventArgs e)
{
String fileName = "SalesOrder_090730120254.xls ";
String path = "D :\\" + fileName;
Response. Cache. SetCacheability (HttpCacheability. Private );
Response. ContentType = "application/vnd. ms-excel ";
Response. AddHeader ("Content-Disposition", "attachment; filename = \" "+ fileName + "\"");
Response. TransmitFile (path );
}

Problem summary:
1. IE6 cache Problems
The above code changes the cache settings to HttpCacheability. noCache, IE7, and FF can be downloaded, but IE6 reports the error "Internet Explorer cocould not download" file name "from" server url ". ", which can be downloaded from the IE kernel 360 browser on IE6.
The reason is that when the cache is set to NoCache or expires immediately, the IE6 cache is cleared too quickly. The file is deleted immediately after the "Save as" dialog box is displayed. When you click "save", the file does not exist.
In some cases, setting Response. Buffer to true may also cause this error.
Direct the link address to the server-side file download method. If the IIS account (anonymous account or integrated authentication account) does not have the NT permission to download the file, an error may also be reported, the download method provided here does not exist.
On the IE6 server, use Fiddler to monitor the Download Status of the 360 browser. It is found that the 360 browser will send an extra request to return the file. The problem is clear. 360 found that the file in the cache does not exist when saving the file, re-request the file from the server. Setting 360 to use the IE download tool also fails to be downloaded.
2. File name problems
When the file name in content-disposition contains Chinese characters, you must use Server. UrlEncode (fileName) to encode the file name. Otherwise, the client will see garbled characters. However, after UrlEncode encoding is used, IE7 correctly displays the file name, and FF cannot correctly decode the Chinese file name.
Ie6is the name displayed on the aspxpage. In this example, the downloaded file name of ie6is test.xls. no solution is found.
Therefore, it is best to name the file name with letters and numbers.
3. the download page is not reflected.
It may be blocked by anti-virus and firewall software. disable anti-virus software and try again (some anti-virus software can disable Web monitoring and other functions)

Contact Us

The content source of this page is from Internet, which doesn't represent Alibaba Cloud's opinion; products and services mentioned on that page don't have any relationship with Alibaba Cloud. If the content of the page makes you feel confusing, please write us an email, we will handle the problem within 5 days after receiving your email.

If you find any instances of plagiarism from the community, please send an email to: info-contact@alibabacloud.com and provide relevant evidence. A staff member will contact you within 5 working days.

A Free Trial That Lets You Build Big!

Start building with 50+ products and up to 12 months usage for Elastic Compute Service

  • Sales Support

    1 on 1 presale consultation

  • After-Sales Support

    24/7 Technical Support 6 Free Tickets per Quarter Faster Response

  • Alibaba Cloud offers highly flexible support services tailored to meet your exact needs.