FireFox下载文件时文件名乱码的解决办法

           之前在写下载文件的时候用的是下面绿色部分的代码,在chrome和360浏览器下都是正常的,但是在firefox下却是乱码(编码)的情况,之前在网上找到相应的文章,红色部分代码为后加上去的,放上去之后确实OK,filename为我们要下载的文件的文件名(包含后缀),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();

猜你喜欢

转载自blog.csdn.net/tengqingyong/article/details/79943971