国产探花免费观看_亚洲丰满少妇自慰呻吟_97日韩有码在线_资源在线日韩欧美_一区二区精品毛片,辰东完美世界有声小说,欢乐颂第一季,yy玄幻小说排行榜完本

首頁 > 學院 > 開發設計 > 正文

文件下載程序中文件名過長的問題

2019-11-18 12:01:34
字體:
來源:轉載
供稿:網友

  今天測試文件下載程序中發現的文件名過長的問題 ,居然發現文件名編碼后長度超過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();
  }
  }

發表評論 共有條評論
用戶名: 密碼:
驗證碼: 匿名發表
主站蜘蛛池模板: 岑溪市| 广丰县| 来宾市| 同仁县| 仲巴县| 师宗县| 东兴市| 天长市| 永德县| 军事| 松原市| 庆安县| 太仆寺旗| 阿尔山市| 通海县| 霍邱县| 南江县| 潜山县| 淮北市| 宿松县| 盱眙县| 离岛区| 连江县| 木兰县| 新蔡县| 雷波县| 清水县| 凤阳县| 石楼县| 长沙市| 博罗县| 广水市| 柘荣县| 枣强县| 古田县| 大渡口区| 汪清县| 巢湖市| 永定县| 荣成市| 阳曲县|