.NET开发

本类阅读TOP10

·NHibernate快速指南(翻译)
·vs.net 2005中文版下载地址收藏
·【小技巧】一个判断session是否过期的小技巧
·VB/ASP 调用 SQL Server 的存储过程
·?dos下编译.net程序找不到csc.exe文件
·通过Web Services上传和下载文件
·学习笔记(补)《.NET框架程序设计(修订版)》--目录
·VB.NET实现DirectDraw9 (2) 动画
·VB.NET实现DirectDraw9 (1) 托管的DDraw
·建站框架规范书之——文件命名

分类导航
VC语言Delphi
VB语言ASP
PerlJava
Script数据库
其他语言游戏开发
文件格式网站制作
软件工程.NET开发
今天测试文件下载程序中发现的文件名过长的问题

作者:未知 来源:月光软件站 加入时间:2005-2-28 月光软件站

居然发现文件名编码后长度超过155就会不能正确显示和下载,最后只好找了这样一个折中的方法,截短了
下面是那里的代码
/// <summary>
  /// 下载附件。
  /// </summary>
  /// <param name="fileName">文件名</param>
  /// <param name="path">文件路径</param>
  public static void DownLoadFileAttachment(string fileName , string path)
  {
   if (System.IO.File.Exists(path))
   {
    try
    {
     fileName  = fileName.Trim();

     for (int  i = 0 ; i < System.IO.Path.InvalidPathChars.Length ; i ++)
     {
      fileName = fileName.Trim().Replace(System.IO.Path.InvalidPathChars[i].ToString() , string.Empty);
     }

     fileName  = fileName.Replace(System.IO.Path.PathSeparator.ToString() , string.Empty);
     
     int maxLength = 155;

     int length  = HttpUtility.UrlEncode(fileName).Length;
     while (length > maxLength)
     {
      int index = fileName.LastIndexOf(".");
      if (index > 0)
      {
       fileName = fileName.Substring(0 , index - 1) + fileName.Substring(index);
      }
      else
      {
       fileName = fileName.Substring(0 , fileName.Length - 1);
      }
      length  = HttpUtility.UrlEncode(fileName).Length;
     }

     System.IO.FileInfo file = new System.IO.FileInfo(path);
     HttpContext.Current.Response.Clear();
     HttpContext.Current.Response.AppendHeader("Content-Disposition", "attachment; filename=" + HttpUtility.UrlEncode(fileName));
     HttpContext.Current.Response.AppendHeader("Content-Length", file.Length.ToString());
     HttpContext.Current.Response.ContentType = "application/octet-stream";
     HttpContext.Current.Response.WriteFile(file.FullName);
     HttpContext.Current.Response.End();
    }
    catch
    {
    }
   }
   else
   {
    HttpContext.Current.Response.Clear();
    DisplayNoFileMessage();
    HttpContext.Current.Response.End();
   }
  }




相关文章

相关软件