为什么下载不适用于文件

本文关键字:文件 适用于 不适用 下载 为什么 | 更新日期: 2023-09-27 18:33:04

我在 asp.net 中签名文件上传,它可以工作。

我正在使用此代码下载

 if (e.CommandName == "Download")
        {
            try
            {
                string filePath = Convert.ToString(e.CommandArgument);
                string fullFilePath = Server.MapPath("~/SiteImages/" + filePath);
                Response.Clear();
                Response.ClearHeaders();
                Response.ClearContent();
                Response.AddHeader("Content-Disposition", "attachment; filename=" + Path.GetFileName(fullFilePath));
                Response.ContentType = ContentType;
                Response.TransmitFile(fullFilePath);
            }
            catch (Exception ex)
            {
            }
            finally { }
        }

我正在上传PDF, .xlxs, .jpg, .mp4, .docx然后下载它们,但其中一些在下载后有效,有些则不然,即 .xls, .docx不起作用并给出错误,即文件已损坏

为什么下载不适用于文件

使用以下

代码:

Response.ContentType = "application/octet-stream";
Response.AppendHeader("Content-Disposition", "attachment; filename="+Path.GetFileName(fullFilePath));
Response.TransmitFile(Server.MapPath("~/SiteImages/" + filePath);
Response.End();

希望对你有帮助

   private byte[] ReadWholeArray(string fileName)
      {
           using (FileStream sourceFile = new FileStream(fileName, FileMode.Open, FileAccess.Read)) {
           byte[] output = new byte[sourceFile.Length];
          if (!sourceFile.CanRead) {
            sourceFile.Read(output, 0, sourceFile.Length);
            return output;
            } 
          else
          {
              throw new ApplicationException("No data can be read!");
            }
          }
       }
   public static string GetMimeTypeByFileName(string sFileName)
      {
      string sMime = "application/octet-stream";
   string sExtension = System.IO.Path.GetExtension(sFileName);
   if (!string.IsNullOrEmpty(sExtension)) {
    sExtension = sExtension.Replace(".", "");
    sExtension = sExtension.ToLower();
    if (sExtension == "xls" || sExtension == "xlsx") {
        sMime = "application/ms-excel";
    } else if (sExtension == "doc" || sExtension == "docx") {
        sMime = "application/msword";
    } else if (sExtension == "ppt" || sExtension == "pptx") {
        sMime = "application/ms-powerpoint";
    } else if (sExtension == "rtf") {
        sMime = "application/rtf";
    } else if (sExtension == "zip") {
        sMime = "application/zip";
    } else if (sExtension == "mp3") {
        sMime = "audio/mpeg";
    } else if (sExtension == "bmp") {
        sMime = "image/bmp";
    } else if (sExtension == "gif") {
        sMime = "image/gif";
    } else if (sExtension == "jpg" || sExtension == "jpeg") {
        sMime = "image/jpeg";
    } else if (sExtension == "png") {
        sMime = "image/png";
    } else if (sExtension == "tiff" || sExtension == "tif") {
        sMime = "image/tiff";
    } else if (sExtension == "txt") {
        sMime = "text/plain";
    }
}
return sMime;
}

//-----法典

       System.Web.HttpContext context = System.Web.HttpContext.Current;
       context.Response.Clear();
       context.Response.ClearHeaders();
       context.Response.ClearContent();
       context.Response.AppendHeader("content-length", fileBytes.Length.ToString());
       context.Response.ContentType = GetMimeTypeByFileName(sFileName);
       context.Response.AppendHeader("content-disposition", Convert.ToString("attachment; filename=") + sFileName);
       context.Response.BinaryWrite(ReadWholeArray(FullFilePath));

这将适用于现代IE