2011-10-06 29 views
1

我的应用的用户可以通过单击此文件的链接查看其他用户上传的文件。返回文件响应是这样在MVC中完成的:使用IE轻松打开的文件,并非总是用Firefox打开(“内容已损坏”)

public ActionResult GetFile(int id) 
     { 
      Attachment attachment = applicationService.GetAttachment(id); 
      HttpContext.Response.ClearHeaders(); 
      HttpContext.Response.ContentType = attachment.MimeType; 
      HttpContext.Response.AppendHeader("Content-Disposition", "attachment; filename=\"" + attachment.FileName + "\""); 

      return File(attachment.FileContent, attachment.MimeType, attachment.FileName); 
     } 

没什么特别的。它总是在IE中工作,它并不总是在Firefox中工作。即使IE打开它没有问题,一些文件的Firefox说“内容已损坏”。

什么问题?

回答

0

可能此时你已经解决了你的问题 我发现这个代码可以帮助你检查你做什么不同,我还没有尝试过,测试它,让我们知道它是否适合你或请发布您的解决方案:

private static void WriteFile(string fileName, string contentType, string content) 
{ 
    HttpContext context = HttpContext.Current; 
    context.Response.Clear(); 
    context.Response.AddHeader("content-disposition", "attachment;filename=" + fileName); 
    context.Response.Charset = ""; 
    context.Response.Cache.SetCacheability(HttpCacheability.NoCache); 
    context.Response.ContentType = contentType; 
    context.Response.Write(content); 
    context.Response.End(); 
} 

关于。

http://stephenwalther.com

0

你可以试试这个,以及

byte[] bytes = // get byte array for the file 
    Response.Buffer = true; 
    Response.Clear(); 
    Response.ClearContent(); 
    Response.ClearHeaders(); 
    Response.ContentType = "mimeType Here"; 
    Response.AddHeader(
     "Content-Disposition", 
     string.Format("attachment; filename={0}",filename) 
    ); 
    // stream pdf bytes to the browser 
    Response.OutputStream.Write(bytes, 0, bytes.Length); 
    Response.End(); 

如果你知道已经有一个文件,那么你可以只使用

Response.TransmitFile((fileLocation)); 

代替OutputStream.Write

希望这有助于。