下载文件名乱码C#

原代码:

                    HttpContext.Current.Response.Clear();
                    HttpContext.Current.Response.ClearHeaders();
                    HttpContext.Current.Response.Buffer = false;
                    HttpContext.Current.Response.ContentType = "application/octet-stream";
                    string s = string.Empty;
                    //IE中文显示正常,IE6文件名有截断(),火狐不行
                   // string s = HttpUtility.UrlEncode(System.Text.UTF8Encoding.UTF8.GetBytes(onefile.fileTitle + onefile.fileType));
                    //火狐正常,IE乱码
                    //string s = onefile.fileTitle + onefile.fileType;
                    HttpContext.Current.Response.AppendHeader("Content-Disposition", "attachment;filename=" + s);    
                    HttpContext.Current.Response.AppendHeader("Content-Length", DownloadFile.Length.ToString());
                    HttpContext.Current.Response.WriteFile(DownloadFile.FullName);
                    HttpContext.Current.Response.Flush();
                    HttpContext.Current.Response.End();

采用以上两种方式,发现火狐和IE下测试结果如上。

添加如下判断,暂时处理

 //解决方案
                    if (HttpContext.Current.Request.UserAgent.ToLower().IndexOf("firefox") > -1)
                    {
                        s = onefile.fileTitle + onefile.fileType;
                    }
                    else
                    {
                        s = HttpUtility.UrlEncode(System.Text.UTF8Encoding.UTF8.GetBytes(onefile.fileTitle + onefile.fileType));
                    }

参考文章:

http://blog.csai.cn/user1/14698/archives/2006/9355.html

http://www.cnblogs.com/kevinwang/archive/2011/05/23/2054342.html

此条目发表在学习笔记分类目录,贴了, , , 标签。将固定链接加入收藏夹。

发表回复

您的电子邮箱地址不会被公开。 必填项已用*标注