文件下载程序中发现的文件名过长的问题

该博客为转载内容,转载自https://www.cnblogs.com/bobowu/archive/2005/08/14/214580.html ,原内容可能涉及Java和PHP相关信息技术知识。
居然发现文件名编码后长度超过155就会不能正确显示和下载,最后只好找了这样一个折中的方法,截短了
下面是那里的代码。后来查看了RFC 2183 中的说明,有一句这样的NOTE ON PARAMETER VALUE LENGHTS: A short (length <= 78 characters),这个应该是官方的解释吧,不过我用155在IE中没有问题,不过可能是IE另外放宽了限制吧。


今天发现下面的那个155也是不可靠的,今天有个过长的文件中间有个“.”就出问题了,下载时能常显示文件名,下载文件也没有问题,但下载下来后一看文件名居然变成了那些编码的字符串,后来把155改成154或做个检测如果遇到“.”就减少这个数字就不会有问题。

 1ExpandedBlockStart.gifContractedBlock.gif/**//// <summary>
 2InBlock.gif  /// 下载附件。
 3InBlock.gif  /// </summary>
 4InBlock.gif  /// <param name="fileName">文件名</param>
 5ExpandedBlockEnd.gif  /// <param name="path">文件路径</param>

 6None.gif  public static void DownLoadFileAttachment(string fileName , string path)
 7ExpandedBlockStart.gifContractedBlock.gif  dot.gif{
 8InBlock.gif   if (System.IO.File.Exists(path))
 9ExpandedSubBlockStart.gifContractedSubBlock.gif   dot.gif{
10InBlock.gif    try
11ExpandedSubBlockStart.gifContractedSubBlock.gif    dot.gif{
12InBlock.gif     fileName  = fileName.Trim();
13InBlock.gif
14InBlock.gif     for (int  i = 0 ; i < System.IO.Path.InvalidPathChars.Length ; i ++)
15ExpandedSubBlockStart.gifContractedSubBlock.gif     dot.gif{
16InBlock.gif      fileName = fileName.Trim().Replace(System.IO.Path.InvalidPathChars[i].ToString() , string.Empty);
17ExpandedSubBlockEnd.gif     }

18InBlock.gif
19InBlock.gif     fileName  = fileName.Replace(System.IO.Path.PathSeparator.ToString() , string.Empty);
20InBlock.gif     
21InBlock.gif     int maxLength = 155;
22InBlock.gif
23InBlock.gif     int length  = HttpUtility.UrlEncode(fileName).Length;
24InBlock.gif     while (length > maxLength)
25ExpandedSubBlockStart.gifContractedSubBlock.gif     dot.gif{
26InBlock.gif      int index = fileName.LastIndexOf(".");
27InBlock.gif      if (index > 0)
28ExpandedSubBlockStart.gifContractedSubBlock.gif      dot.gif{
29InBlock.gif       fileName = fileName.Substring(0 , index - 1+ fileName.Substring(index);
30ExpandedSubBlockEnd.gif      }

31InBlock.gif      else
32ExpandedSubBlockStart.gifContractedSubBlock.gif      dot.gif{
33InBlock.gif       fileName = fileName.Substring(0 , fileName.Length - 1);
34ExpandedSubBlockEnd.gif      }

35InBlock.gif      length  = HttpUtility.UrlEncode(fileName).Length;
36ExpandedSubBlockEnd.gif     }

37InBlock.gif
38InBlock.gif     System.IO.FileInfo file = new System.IO.FileInfo(path); 
39InBlock.gif     HttpContext.Current.Response.Clear(); 
40InBlock.gif     HttpContext.Current.Response.AppendHeader("Content-Disposition""attachment; filename=" + HttpUtility.UrlEncode(fileName)); 
41InBlock.gif     HttpContext.Current.Response.AppendHeader("Content-Length", file.Length.ToString()); 
42InBlock.gif     HttpContext.Current.Response.ContentType = "application/octet-stream"
43InBlock.gif     HttpContext.Current.Response.WriteFile(file.FullName); 
44InBlock.gif     HttpContext.Current.Response.End(); 
45ExpandedSubBlockEnd.gif    }

46InBlock.gif    catch
47ExpandedSubBlockStart.gifContractedSubBlock.gif    dot.gif{
48ExpandedSubBlockEnd.gif    }

49ExpandedSubBlockEnd.gif   }

50InBlock.gif   else
51ExpandedSubBlockStart.gifContractedSubBlock.gif   dot.gif{
52InBlock.gif    HttpContext.Current.Response.Clear();
53InBlock.gif    DisplayNoFileMessage();
54InBlock.gif    HttpContext.Current.Response.End();
55ExpandedSubBlockEnd.gif   }

56ExpandedBlockEnd.gif  }

57None.gif

转载于:https://www.cnblogs.com/bobowu/archive/2005/08/14/214580.html

评论
添加红包

请填写红包祝福语或标题

红包个数最小为10个

红包金额最低5元

当前余额3.43前往充值 >
需支付:10.00
成就一亿技术人!
领取后你会自动成为博主和红包主的粉丝 规则
hope_wisdom
发出的红包
实付
使用余额支付
点击重新获取
扫码支付
钱包余额 0

抵扣说明:

1.余额是钱包充值的虚拟货币,按照1:1的比例进行支付金额的抵扣。
2.余额无法直接购买下载,可以购买VIP、付费专栏及课程。

余额充值