Custom handler does not like gaps in Firefox

I have a custom handler that implements IHttpHandler. Custom handler allows us to create a dynamic URL-address to download files people.

The code is as follows:

public void ProcessRequest(HttpContext context) { context.Response.AddHeader("Content-Disposition", "attachment;filename=" + attachment.FileName); context.Response.AddHeader("Content-Length", attachment.Fileblob.Length.ToString()); context.Response.ContentType = GetMimeType(attachment.FileName); context.Response.OutputStream.Write(attachment.Fileblob, 0, attachment.Fileblob.Length); } 

The problem with investing. File name. If the file has a space,

filename - 1.bmp

Then, in the Internet explorer it works fine, but in firefox the file download dialog box truncates it so

file name

No extension or anything else. I also tried this,

attachment.FileName.Replace (","% 20 ")

Which works in IE again, but in firefox this causes the file name to be set to this download dialog,

filename% 20-% 201.bmp

I also tried it,

HttpUtility.UrlEncode (attachment.FileName)

In both Firefox and the IE, which leads to it,

filename + - + 1.bmp

Any ideas?

+4
source share
1 answer

Try replacing the actual space characters %20 . Should still work in all browsers.

Edit

Good, so that didn't seem to help. Plan B, then.

Try to adjust our title of Content-Disposition the HTTP, to the name of the file attachment was enclosed in double quotes, for RFC 2231 .

 public void ProcessRequest(HttpContext context) { context.Response.AddHeader("Content-Disposition", String.Format("attachment;filename=\"{0}\"", attachment.FileName)); context.Response.AddHeader("Content-Length", attachment.Fileblob.Length.ToString()); context.Response.ContentType = GetMimeType(attachment.FileName); context.Response.OutputStream.Write(attachment.Fileblob, 0, attachment.Fileblob.Length); } attachment.Fileblob.Length.ToString ()); public void ProcessRequest(HttpContext context) { context.Response.AddHeader("Content-Disposition", String.Format("attachment;filename=\"{0}\"", attachment.FileName)); context.Response.AddHeader("Content-Length", attachment.Fileblob.Length.ToString()); context.Response.ContentType = GetMimeType(attachment.FileName); context.Response.OutputStream.Write(attachment.Fileblob, 0, attachment.Fileblob.Length); } 
+6
source

All Articles