为什么不下载文件工作?

时间:2016-02-02 17:23:42

标签: c# asp.net c#-4.0 file-upload

我在asp.net中使用FileUpload并且它可以工作。

我正在使用此代码下载

 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不起作用并且出错,即文件已损坏

2 个答案:

答案 0 :(得分:0)

Use following code:

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

Hope it helps

答案 1 :(得分:0)

   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;
}

//-----Code

       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));

//This will work modern IE