文件下载

public static void DownloadFile(string filepath)
  {
   string strContentType = "";
   string strFileName  = "";
   string strFilePath  = "";
   int  intIndex1  = -1;
   int  intIndex2  = -1;
   int  intIndex  = 0;

   if (filepath.EndsWith(".doc"))
   {
    strContentType = "application/msword";
   }
   else if (filepath.EndsWith(".xls"))
   {
    //strContentType = "application/x-zaurus-xls"
    strContentType = "application/vnd.ms-excel";
   }
   else if (filepath.EndsWith(".pdf"))
   {
    strContentType = "application/pdf";
   }
   else if (filepath.EndsWith(".ppt"))
   {
    //strContentType = "application/powerpoint"
    strContentType = "application/vnd.ms-powerpoint";
   }
   else if (filepath.EndsWith(".jpg") || filepath.EndsWith(".jpeg") || filepath.EndsWith(".jpe"))
   {
    strContentType = "image/jpeg";
   }
   else if (filepath.EndsWith(".bmp"))
   {
    strContentType = "image/bmp";
   }
   else if (filepath.EndsWith(".gif"))
   {
    strContentType = "image/gif";
   }
   else if (filepath.EndsWith(".zip"))
   {
    strContentType = "application/zip";
   }
   else if (filepath.EndsWith(".txt"))
   {
    strContentType = "text/plain";
   }
   else
   {
    strContentType = "application/octet-stream";
   }

   intIndex1 = filepath.LastIndexOf("/");
   intIndex2 = filepath.LastIndexOf("//");

   if (intIndex1 > intIndex2)
   {
    intIndex = intIndex1;
   }
   else
   {
    intIndex = intIndex2;
   }

   if (intIndex > -1)
   {
    if (intIndex + 1 < filepath.Length)
     strFileName = filepath.Substring(intIndex + 1);
   }
   else
   {
    strFileName = filepath;
   }

   if (filepath.StartsWith("/") || filepath.StartsWith("//"))
    strFilePath = HttpContext.Current.Server.MapPath("/") + filepath;
   else
    strFilePath = filepath;

   if (FileUtil.isFileExist(strFilePath))
   {
    HttpContext.Current.Response.Clear();
    HttpContext.Current.Response.ContentType = strContentType;
    HttpContext.Current.Response.AddHeader("Content-Disposition", "attachment; filename=" + HttpUtility.UrlEncode(strFileName));

    HttpContext.Current.Response.WriteFile(strFilePath);
    //HttpContext.Current.Response.BinaryWrite(objFileIO.IFileByte(strFilePath));

    HttpContext.Current.Response.Flush();
    HttpContext.Current.Response.Close();
   }
   else
   {
    SetGlobalMessageID(FindKeyValueCutNull(GXML.COMMONMESSAGE, GMSG.MSG904));
   }            
  } 

评论
添加红包

请填写红包祝福语或标题

红包个数最小为10个

红包金额最低5元

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

抵扣说明:

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

余额充值