簡體   English   中英

MVC.Net在新窗口中打開文件,而不是下載

[英]MVC.Net Open a file in a new window instead of downloading

我的要求是,單擊下載鏈接后,在新窗口/選項卡中打開文件。 盡管將內容分配設置為“內聯”,但始終會下載該文件,如以下文章中所建議。

如何在新的標簽頁或窗口中打開PDF文件而不是下載文件(使用asp.net)?

我的HTTP響應是

HTTP/1.1 200 OK
Cache-Control: private, s-maxage=0
Content-Type: application/octet-stream
Server: Microsoft-IIS/7.5
X-AspNetMvc-Version: 5.2
content-disposition: inline;filename=FileName.pdf
X-AspNet-Version: 4.0.30319
X-Powered-By: ASP.NET
Content-Length: 141954

我的控制器動作如下-

public async Task<FileContentResult> Index(string fileName)
    {
        byte[] document = await getFileContent("SomePathForTheFile");
        HttpContext.Response.AddHeader("content-disposition", "inline;filename=" + document.FileName);
        return File(document, "application/octet-stream");
    }

建議在新標簽頁/窗口中自動打開文件而不是僅下載文件,這將對您大有幫助! 謝謝!

找到了答案。

要在新窗口中打開文件,必須完成2件事-

1.content-disposition必須設置為inline

2.內容類型應設置為與正在下載的文件相對應的類型-

應用程序/ pdf-pdf文件

application / xml-用於xml文件

application / vnd.ms-excel-用於xls文件等。

請嘗試以下代碼。

@Html.ActionLink("Home","Index",null,new { @target="_blank" })

由於我們將返回查看模式,因此無需提及標頭響應

public async Task<FileContentResult> Index(string fileName)
    {
        byte[] document = await getFileContent("SomePathForTheFile");
        return File(document, "application/octet-stream");
    }

暫無
暫無

聲明:本站的技術帖子網頁,遵循CC BY-SA 4.0協議,如果您需要轉載,請注明本站網址或者原文地址。任何問題請咨詢:yoyou2525@163.com.

 
粵ICP備18138465號  © 2020-2024 STACKOOM.COM