之前在写下载文件的时候,在chrome和360浏览器下都是正常的,但是在firefox下却是乱码(编码)的情况。RFC 2183规定filename只能为US-ASCII码,然而现代浏览器中许多已经支持UTF-8编码了,但各个浏览器的支持规则不同。在IE、chrome中,可以直接用filename作为下载文件的名称,但是Firefox却不支持这样。
this.context.Response.Charset = "UTF-8";
this.context.Response.ContentEncoding = System.Text.Encoding.UTF8;
string filename = HttpUtility.UrlEncode(nvcOriginalName.Trim(), System.Text.Encoding.UTF8);
if (this.context.Request.UserAgent.ToLower().IndexOf("firefox") > -1) {
this.context.Response.AddHeader("Content-Disposition", "attachment; filename*=UTF-8''" + filename);
} else {
this.context.Response.AddHeader("Content-Disposition", "attachment;filename=" + filename);
}
this.context.Response.AddHeader("Content-Length", file.Length.ToString());
this.context.Response.ContentType = MimeType.GetMimeTypeFromPath(path);
this.context.Response.WriteFile(file.FullName);
this.context.Response.Flush();
this.context.Response.Clear();