Notes for dynamically generating Excel files for Browser Download

Source: Internet
Author: User

There was a software project recently. The customer found that IE6 opened the Excel file on the page and saw garbled characters.

After investigation, we found that IE6 has some strange ways of working, which can be regarded as a bug.

When a problem occurs, we use Firefox's plug-in firebug to find that we set the HTTP Response Header improperly:

Response. setcontenttype ("text/CSV ");// The error occurs in this row.

// The followingCodeSet the file name. The IE and non-ie browsers process different Chinese file names.

String newfilename2 = NULL;
String useragent = getuseragent (request );
If (useragent. indexof ("MSIE")> = 0 ){
Urlcodec codec = new urlcodec ();
Newfilename2 = codec. encode (filename, "UTF-8 ");
} Else {
Newfilename2 = new string (filename. getbytes ("UTF-8"), "ISO-8859-1 ");
}
If (filename. tolowercase (). indexof (". htm") <0 ){
Response. setheader ("content-disposition", "attachment; filename = \" "+ newfilename2 + "\"");
}

The above code runs normally on IE6, ie9, and firefox5. However, the customer reported that there were garbled characters on IE6.

After repeated tests, we found that:

If you enter

Http: // localhost/ctms/web_dispatcher/common/export_binary_file? UUID = 1e48cbd96d1b4b25b64d5df242580d27

In the browser, a "open" and "save" dialog box is displayed. If you select "open", the garbled window is opened directly in the browser. This line of code is invalid:

Response. setheader ("content-disposition", "attachment; filename = \" "+ newfilename2 + "\"");

However, if you use JavaScript to navigate the current page to the preceding URL, IE6 may be garbled and IE6 may not be garbled.

After a Google search on the Internet, I found that the web system dynamically generated an Excel file, which many people encountered in IE6.

First, let's get rid of the error line:

Response. setcontenttype ("text/CSV ");==> Response. setcontenttype ("Application/vnd. MS-Excel");

Test results:

Chrome, firefox5, and ie9 are both normal. IE6 can be downloaded normally. garbled characters are not displayed in the current browser window, but the file name in the header cannot be obtained. Instead, the file name is obtained from the URL, the result is:Export_binary_file.xls.

Note that our dynamic website is:

Http: // localhost/ctms/web_dispatcher/common/Export_binary_file? UUID = 1e48cbd96d1b4b25b64d5df242580d27

This is strange. IE6 extracts the First Half of the file name from the URL and automatically adds the. xls suffix to the new file name.

In the future, we may generate an Excel 2007 or an Excel 2010 file"Application/vnd. MS-Excel"Not suitable.

I tried two other methods:

A.Response. setcontenttype ("application/octet-stream ");

// This comes from http://www.ietf.org/rfc/rfc1521.txt

Result:

Chrome/firefox5/ie9 are normal. Enter the URL in the address bar of IE6 and press Enter. The file name in the header cannot be obtained, but the file name is obtained from the URL. The result is:Export_binary_file. Compared with the above error in vnd. MS-Excel, the suffix of. xls is missing.

B.Response. setcontenttype ("application/force-download ");

Result:

Chrome/firefox5/ie9 are normal. Enter the URL in the address bar of IE6 and press Enter. The result is:Export_binary_file. Same as.

Conclusion:

IE6 uses JavaScript to navigate to the dynamically generated Excel website. The file name is different from the one directly entered when the website is saved. This should be a bug.

ChangeResponse. setcontenttype ("text/CSV"); solves the garbled problem and cannot completely solve the file name problem.

Speculative reason:

In the era before dynamic web page technology, static Web pages are used. The URLs for File Download are usually as follows:

Http://velocityweb.sourceforge.net/zsso_files/zsso-v2.0.1.zip

When the browser saves the downloaded file, it is normal to retrieve the file name from the URL.

In the era of dynamic web page technology, the website looks like this:

Http: // localhost/ctms/web_dispatcher/common/export_binary_file? UUID = 1e48cbd96d1b4b25b64d5df242580d27

Then, getting the file name from the URL will cause a mess.

IE6 jumps to the above URL in Javascript, which is normal. If you directly enter the above URL, the file name is abnormal. This should be because IE6 has not been completely changed. The latest browsers, ie9, firefox5, and chrome, are normal. Due to the time relationship, we did not test earlier versions of Firefox/chrome browsers.

We recommend that you install Firefox 5.

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.