首页 新闻 会员 周边

文件名过长,下载文件名被截短

0
悬赏园豆:20 [已关闭问题]

最近在测试中发现一个下载问题,就是一个文件名的超过18个中文字就会被截断,网上也查了,没看到解决方案,不能折中截断必须要文件名全,我试过163的附件名称超过18个中文字也可以,下载的时候不会被截断
我把我的代码贴出来请各位大侠看看,这个是下载的方法

1 #region 文件下载
2 private bool file_down(string bill, ref string arg_msg)
3 {
4 bool rslt = true;
5 try
6 {
7 if (bill == null && bill.Trim().Length > 0)
8 {
9 rslt = false;
10 arg_msg = "下载失败";
11 goto ext_file_down;
12 }

14 Byte[] bytes;
15 string ls_down_file = "select * from permit_msg_image WHERE billid=" + bill;
16 DataTable dt = cs_con_image.Select(ls_down_file, null, ref arg_msg).Tables[0];
17 bytes = (Byte[])(dt.Rows[0]["add_file"]);
18 string fileName = dt.Rows[0]["file_name"].ToString().Trim();
19 Response.ClearContent();
20 Response.ClearHeaders();





26 Response.ContentType = "application/octet-stream"; // 设置输出流的Http MIME类型//通知浏览器下载文件而不是打开
27   Response.AddHeader("Content-Disposition", "attachment; filename=" + HttpUtility.UrlEncode(fileName, System.Text.Encoding.UTF8)); //fileName为需要下载的文件名



32 Response.BinaryWrite(bytes); // 写入输入流
33 Response.Flush(); // 向客户端发送数据流
34 Response.End();
35 }
36 catch (Exception ex_file_down)
37 {
38 rslt = false;
39 arg_msg = "下载失败:" + ex_file_down.Message;
40 goto ext_file_down;
41 }
42 ext_file_down: ;
43 return rslt;
44 }
45 #endregion

 




网上都说是因为
 Response.AddHeader("Content-Disposition", "attachment; filename=" + HttpUtility.UrlEncode(fileName, System.Text.Encoding.UTF8)); //fileName为需要下载的文件名
这个的原因才使用UTF8的原因截取了,请大家看看是否是这样
谢谢了

廖雪萍的主页 廖雪萍 | 初学一级 | 园豆:88
提问于:2010-08-10 10:43
< >
分享
其他回答(2)
0

你可以参考下这篇文章,将文件名截短:

http://www.cnblogs.com/bobowu/archive/2005/08/14/214580.html

kyo-yo | 园豆:5587 (大侠五级) | 2010-08-10 10:49
不行呀,不能用折中的方法 163的超过18个中文字的同样可以下载下来 折中的方法网上很多呀!
支持(0) 反对(0) 廖雪萍 | 园豆:88 (初学一级) | 2010-08-10 10:54
0

Content-Disposition 对 fileName 有长度限制,所以,汉字编码成 UTF8 就容易超长,解决的办法就是,用GB2312编码,

HttpUtility.UrlEncode(fileName, System.Text.Encoding.GetEncoding("GB2312"))

Launcher | 园豆:45045 (高人七级) | 2010-08-10 15:07
不行呀,是乱码哦!这个我今天上午的时候就已经试过了
支持(0) 反对(0) 廖雪萍 | 园豆:88 (初学一级) | 2010-08-10 15:42
0

/// <summary> hac_ExportExcel1: 导出长文件名Excel文件.        ///  应用示例:        ///    string SourceFile1; // 在网站服务器上的源文件,例:~/TempFile\\1.xls         ///    string TargetFile1; // 输出的Excel文件名,注意不包括扩展名 .xls,例:【0122】福州商贸职业中专学校2006级秋季初中后毕业生名单        ///    ha1.hac_ExportExcel1 e1 = new ha1.hac_ExportExcel1();        ///   e1.ExportExcel1(this.Page,SourceFile1, TargetFile1);</summary>        ///           public void ExportExcel1(System.Web.UI.Page Page1,string SourceFile1,string TargetFile1)        {            Page1.Response.HeaderEncoding = System.Text.Encoding.GetEncoding("GB2312");            Page1.Response.AppendHeader("Content-Disposition", "attachment;filename=" + TargetFile1 + ".xls");            Page1.Response.ContentEncoding = System.Text.Encoding.UTF8;            Page1.Response.ContentType = "application/ms-excel";//设置输出文件类型为excel文件。            Page1.Response.Clear();            Page1.Response.Buffer = true;
            //string filePath = Server.MapPath("~/TempFile\\1.xls");            string filePath = Page1.Server.MapPath("~/"+SourceFile1);            string path = filePath;            System.IO.FileInfo file = new System.IO.FileInfo(path);            Page1.Response.WriteFile(file.FullName);            Page1.Response.Flush();            Page1.Response.End();        }

以上是我用的导出Excel时下载的方法,试试看吧

LiecoLin | 园豆:210 (菜鸟二级) | 2010-08-11 11:45
清除回答草稿
   您需要登录以后才能回答,未注册用户请先注册