2016-02-13 33 views
0

我试图在视图中打开文件而不是下载它。该代码在IE和Firefox中正常工作,但它在Chrome中下载文件而不是打开它。为什么在ASP.NET MVC应用程序中通过Action打开文件可以在IE和Firefox中使用,但不能在Chrome中使用?

这是我的代码。

//OpenWord Action 
     string filePath = @"G:\WordDemo\DocXExample.doc"; 
     var doc = DocX.Create(fileName) 
     doc.InsertParagraph("This is my first paragraph"+person.FirstName+" "+person.LastName); 
     doc.Save(); 

     string fileName = "DocXExample.doc"; 
     byte[] fileData = System.IO.File.ReadAllBytes(filePath); 
     string contentType = MimeMapping.GetMimeMapping(filePath); 
    string mimeType = "application/msword"; 
Response.AppendHeader("Content-Disposition", "inline; filename=" + fileName); 
return File(fileData, mimeType); 

欢迎任何帮助和建议。

+2

请问您的Chrome浏览器版本有一个MS Word的观众? – wimh

+0

@Wimmel:感谢评论,但IE和Firefox正在使用系统的MS Word应用程序,所以我不认为这是问题。 –

+1

@PratikPatil因此,尽管您的问题标题询问“在浏览器中打开文件”,并声称在IE和Firefox中可用,但您现在在评论中说,该文件从不在任何浏览器中打开,而始终在单独的应用程序中打开,那也是你在Chrome中寻找的东西?这是完全不同的问题。我强烈建议编辑你的问题,清楚地问你在做什么。 – hvd

回答

0

它在设备和客户端之间并不一致,因为它依赖于为客户端安装的插件。

试试这个。

主机在您的网站上的word文档所以它是可供下载通过比如说http://www.example.com/path/mydocument.doc

在您的视图中添加此iframe

<iframe src="http://docs.google.com/gview?url=http://www.example.com/path/mydocument.doc&embedded=true"></iframe> 
+0

我试过这样的东西,但它没有奏效。 '