English 中文(简体)
自定义处理程序不喜欢Firefox上的空格
原标题:Custom Handler Doesn t Like Spaces On Firefox
  • 时间:2011-05-26 04:54:09
  •  标签:
  • c#
  • asp.net

我有一个实现IHttpHandler的自定义处理程序。自定义处理程序允许我们生成一个动态URL供人们下载文件。

代码如下所示,

public void ProcessRequest(HttpContext context)
{
    context.Response.AddHeader("Content-Disposition", "attachment;文件名=" + 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);
}

附件.Filename有问题。如果文件名恰好有这样的空格,

文件名-1.bmp

然后在internet explorer上它工作得很好,但在firefox上,文件下载对话框将其截断为这样,

文件名

没有延期或其他任何事情。我也试过这个,

attachment.FileName.Replace(“”,“%20”)

Which works in IE again, but in firefox it results in the 文件名 being set to this in the download dialog,

文件名%20-%201.bmp

我也试过这个,

HttpUtility.UrlEncode(附件.FileName)

在导致这种情况的firefox和IE中,

文件名+-+1.bmp

有什么想法吗?

最佳回答

请尝试将实际的空格字符替换为%20。应该仍然适用于所有浏览器。

编辑

好吧,所以这似乎没有帮助。那就B计划吧。

让我们尝试调整我们的Content-DispositionHTTP标头,以便附件文件名用双引号括起来,根据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);
}
问题回答

暂无回答




相关问题
Anyone feel like passing it forward?

I m the only developer in my company, and am getting along well as an autodidact, but I know I m missing out on the education one gets from working with and having code reviewed by more senior devs. ...

How to Add script codes before the </body> tag ASP.NET

Heres the problem, In Masterpage, the google analytics code were pasted before the end of body tag. In ASPX page, I need to generate a script (google addItem tracker) using codebehind ClientScript ...

Transaction handling with TransactionScope

I am implementing Transaction using TransactionScope with the help this MSDN article http://msdn.microsoft.com/en-us/library/system.transactions.transactionscope.aspx I just want to confirm that is ...

System.Web.Mvc.Controller Initialize

i have the following base controller... public class BaseController : Controller { protected override void Initialize(System.Web.Routing.RequestContext requestContext) { if (...

Microsoft.Contracts namespace

For what it is necessary Microsoft.Contracts namespace in asp.net? I mean, in what cases I could write using Microsoft.Contracts;?

Separator line in ASP.NET

I d like to add a simple separator line in an aspx web form. Does anyone know how? It sounds easy enough, but still I can t manage to find how to do it.. 10x!

热门标签