When downloading a file with Response.Write spaces in the file name are replaced with underscores, and when the associated application opens, a number in square brackets is appended:
Response.AppendHeader("Content-disposition", "attachment; filename=this is the file.xml");
Response.Write(dr["InfopathDoc"]);
This results in this file name in the associated application:
this_is
_the_
file[1].xml
How can I get rid of the underscores and why do I get the [1] ?
Thanks
Even now, putting a space in the name of a file that is destined to be on a website URL isn't a good idea. And so, some people will put underscores (_) or hyphens ➖ instead of spaces in filenames to help avoid these issues. Because then they don't have to quote their paths on the command line.
Avoid the Underscores “Although OS X and Mac OS formatted disks support spaces in filenames, certain processing scripts and applications may not recognize these characters, or may treat your files differently than expected. Consider substituting an underline (_) or dash (-) where you would normally use spaces.”
Illegal Filename Characters Keep your filenames to a reasonable length and be sure they are under 31 characters. Most operating systems are case sensitive; always use lowercase. Avoid using spaces and underscores; use a hyphen instead.
Found the solution for this problem here
http://dotnetslackers.com/Community/blogs/kaushalparik/archive/2009/05/06/file-download-problem-filename-with-spaces-truncated-in-ff-and-replaced-by-underscore-in-ie.aspx
To solve problem for FF, add quotes around the filename as
Response.AddHeader("Content-Disposition", "attachment; filename=\"" + filename + "\"");
And for IE, replace spaces with '%20'
filename = toDownload.Name.Replace(" ", "%20");
If anyone is still interested:
This kind of file name rewriting is done by the browser only as you can see when inspecting the received HTTP response with an appropriate tool, independent of the browser used. IE first downloads the requested file into its "Temporary Internet Files" folder system (which is not just one, but this is another topic), and for this purpose the file receives a new name, making best match to the "Content-Disposition" suggestion from the HTTP response. But if an equally named file is already present in the actual "Temporary Internet Files" folder actually used for this file, its name is extended by a sequenced number in brackets, like "[2]". Because every new HTTP request causes the IE cache mechanism to newly compute the actual cache folder, and the next cache folder chosen may not yet contain a file with that name, the number may seem to vanish at the next download of a file or resource with the same name.
If a downloaded file is stored somewhere, the originally suggested file name is used again usually, depending on the IE version. Some versions and patch levels seem to use the cache folder file name instead :-(
The problem starts to become annoying when the browser hands out a downloaded file to a chosen or automatically selected application. In this case the application is called to open the file directly from the cache, which is bad for at least 2 reasons:
(1) The file name will be the name of the file in the cache folder, not the suggested name. This may even strip the extension, which will confuse some applications even if they have been chosen for handling the file correctly.
(2) If an internet newbie user downloads and opens a file for editing and then simply presses the "Save" button of the application, the file is simply saved into the IE cache folder, and no user of this kind will ever find this file again. These are things which can turn people really angry and desperate...
If you love us? You can donate to us via Paypal or buy me a coffee so we can maintain and grow! Thank you!
Donate Us With