How to get around IE error [1] when saving excel file from web server?

I noticed that Internet Explorer appends the number in square brackets to files downloaded from the Internet (usually [1]). This poses a big problem when loading Excel spreadsheets, since the square brackets are not a valid character for the file name inside the name of the Excel worksheet. This problem is specific to IE, other browsers keep the same file name.

So, if you have a pivot table that is automatically updated when you open the file, you will receive an error message stating that the name "file [1] .yourPivotTableName" is not valid.

Is there any solution to this problem?

EDIT: It seems that regardless of the file name suggested by the HTTP directives , IE adds [1] in all cases, which causes a problem! (So ​​answers about file names do not help in this case)

EDIT: I tried using VBA code to save the file under a different name when it opens. However, it does not work (same error message as before). Do you think there is a way to fix this with VBA?

+4
source share
10 answers

It works for me using the VBA provided by this cool guy (think of him gently). It renames the file and then reattaches the anchor points.

http://php.kennedydatasolutions.com/blog/2008/02/05/internet-explorer-breaks-excel-pivot-tables/

+2
source

I think this happens when you open a spreadsheet in IE, and IE saves it in a temporary file. And I think this only happens when the spreadsheet file name has more than one dot in it. Try this with the simple "sample.xls". Another workaround is to tell users to save the file to the desktop and then open it.

+2
source

This is a built-in feature in Internet Explorer.

Stop using "Open", start using "Save" in the file download window, otherwise IE will add "[1]" to the name of the file that it places in some temporary folder.

You can create some .NET application using System.IO.FileSystemWatcher , which catches the event of creating a downloaded file or something else, and renames the file.

+2
source

I solved this problem using a method in which we pass 3 parameters: file name, file extension (without .dot) and HTTP request); then performs UTF-8 encoding of the file name and extension. Code example:

public static String encoding(String fileName, String extension, HttpServletRequest request) { String user = request.getHeader( "user-agent" ); boolean isInternetExplorer = ( user.indexOf( "MSIE" ) > -1 ); String var = ""; try { fileName = URLEncoder.encode( fileName, "UTF-8" ); fileName = fileName.trim().replaceAll( "\\+", " " ); extension = URLEncoder.encode( extension, "UTF-8" ); extension = extension.trim().replaceAll( "\\+", " " ); if ( isInternetExplorer ) { disposition = "attachment; filename=\"" + fileName+"."+extension+"\""; } else { var = "attachment; filename*=UTF-8''" + fileName+"."+extension; } } catch ( UnsupportedEncodingException ence ) { var = "attachment; filename=\"" + fileName+"."+extension; ence.printStackTrace(); } return var; } 

In my case, it worked. Hope this helps all of you.

+1
source

Actually, the correct .NET code is as follows:

 Response.AppendHeader("content-disposition", "attachment;filename=file.xls"); Response.ContentType = "application/vnd.ms-excel"; 

Note: AppendHeader , not AddHeader , which I think only works on the debug web server and IIS7.

0
source

The following worked for me:

 private string EncodeFileName(string fileName) { fileName = HttpUtility.UrlEncode(fileName, Encoding.UTF8).Replace("+", " "); if (HttpContext.Current.Request.UserAgent.ToLower().Contains("msie")) { var res = new StringBuilder(); var chArr = fileName.ToCharArray(); for (var j = 0; j < chArr.Length; j++) { if (chArr[j] == '.' && j != fileName.LastIndexOf(".")) res.Append("%2E"); else res.Append(chArr[j]); } fileName = res.ToString(); } return "\"" + fileName + "\""; } 
0
source

You can just make sure that auto-update is disabled in the settings window for rotation. Now, even when it opens from the server, the core will work fine

0
source

Put these four lines in your code:

 response.reset(); response.setHeader("Expires", "0"); response.setHeader("Cache-Control","must-revalidate,post-check=0, pre-check=0"); response.setHeader("Pragma", "public"); 

Hope this helps.

0
source

I ran into the same problem and came up with (imo) a better solution that does not need VBA.

If you set the "Content-Disposition" header to "attachment; filename = <...>" instead of "inline; filename = <...>", ordinary browsers will open a dialog box that allows you to save or open the file with the file name, defined in the title, but Internet Explorer will behave somehow weird. It will open the file download dialog, and if you click "Save", it will offer the file name defined in the header, but if you click "Open", it will save the file to a temporary folder and open it with a name that matches your URN ( without a "namespace"), for example if your URI is http: //server/folder/file.html , so IE will save your file as file.html (without brackets, woo hoo!). This leads to a solution:

Write a script that processes the request http: // server / folder / *, and when you need to submit the XLS file, just redirect it to the script (use the name of your file instead of an asterisk) with Content-Disposition set to a string.

0
source

In .NET I found from experience only this seems to work for me:

  Response.AddHeader("Content-Disposition", "attachment; filename=excel.xls"); Response.AddHeader("Content-Type", "application/vnd.ms-excel"); Response.ContentType = "application/vnd.ms-excel"; 

Duplication smells, but so far I still have not reached the end (perhaps explains the Sebs post). In addition, the value of "content-Disposition" looks very subtle, using a: instead of a; or omit the space between it and "filename" and it blows!

Also, if compression is enabled in IIS, this can help you:

 Response.ClearHeaders() 
-1
source

Source: https://habr.com/ru/post/1277066/


All Articles