我想在瀏覽器窗口中打開我的word和PDF文檔進行顯示。但是,我的PDF文檔開頭正常,但文檔文檔失敗。以下是我的代碼。在瀏覽器中打開word文檔無法正常工作--asp.net mvc
public ActionResult ViewResume(long userid)
{
string FileName = getResumeName(userid);
string path = System.IO.Path.Combine(Server.MapPath("~/Content/files/"), FileName);
var fileStream = new FileStream(path,
FileMode.Open,
FileAccess.Read
);
FileInfo info = new FileInfo(FileName);
string extension = info.Extension;
FileStreamResult fsResult;
if (info.Extension == ".pdf")
fsResult = new FileStreamResult(fileStream, "application/pdf");
else
fsResult = new FileStreamResult(fileStream, "application/vnd.ms-word");
return fsResult;
}
當我嘗試打開Word文檔時,它會替我下載一個.exe文件。我不確定問題是什麼。它打開PDF沒有問題。請讚賞任何幫助。
UPDATE
我懷疑的MIME類型。我不確定哪個是正確的文檔。
這是mime類型,試試這個一個代替'application/vnd.openxmlformats-officedocument.wordprocessingml.document' – 2015-04-05 15:07:17
Nativly它不支持在瀏覽器中查看Microsoft Word文檔。有幾個選項:將Word文檔轉換爲PDF或使用Word Viewer進行MVC。爲了下載文件,Alundra的答案是正確的。 – 2015-04-05 15:14:59