2016-08-11 25 views
10

WebAPIコントローラからファイルを返しています。 Content-Dispositionヘッダーの値は自動的に「添付ファイル」に設定されます。例:ASP.Netコアコンテンツ処理添付ファイル/インライン

処分:添付; filename = "30956.pdf";ファイル名* = UTF-8''30956.pdf

添付ファイルに設定すると、ブラウザはファイルを開くのではなく保存するように求めます。私はそれを開いてほしいです。

「添付」ではなく「インライン」に設定するにはどうすればよいですか?

私は、このメソッドを使用してファイルを送信しています:

public IActionResult GetDocument(int id) 
{ 
    var filename = $"folder/{id}.pdf"; 
    var fileContentResult = new FileContentResult(File.ReadAllBytes(filename), "application/pdf") 
    { 
     FileDownloadName = $"{id}.pdf" 
    }; 
    // I need to delete file after me 
    System.IO.File.Delete(filename); 

    return fileContentResult; 
} 

答えて

1

は、私が見つけた最良の方法は、手動でのコンテンツ配置ヘッダを追加することですHttpResponseMessage

public IActionResult GetDocument(int id) 
{ 
    var filename = $"folder/{id}.pdf"; 

    Response.Headers["Content-Disposition"] = $"inline; filename={id}.pdf"; 
    var fileContentResult = new FileContentResult(System.IO.File.ReadAllBytes(filename), "application/pdf") 
    { 
     FileDownloadName = $"{id}.pdf" 
    }; 
    // I need to delete file after me 
    System.IO.File.Delete(filename); 

    return fileContentResult; 
} 
+0

私はASP.Net Coreが必要です。これはASP.Netオブジェクトを返します。それを試してJSONシリアル化されたHttpResponseMessageオブジェクトを返します。 –

+0

私の回答を更新 – Jag

+2

これを追跡するために、次の問題を作成しました:https://github.com/aspnet/Mvc/issues/5133 –

20

でそれを試してみてください。あなたは(さまざまなFile(byte[]...)オーバーロードを使用して、またはFileContentResultを使用して)バイト配列に一度メモリ内のファイルを読みたくない考える

private IActionResult GetFile(int id) 
{ 
     var file = $"folder/{id}.pdf"; 

     // Response... 
     System.Net.Mime.ContentDisposition cd = new System.Net.Mime.ContentDisposition 
     { 
       FileName = file, 
       Inline = displayInline // false = prompt the user for downloading; true = browser to try to show the file inline 
     }; 
     Response.Headers.Add("Content-Disposition", cd.ToString()); 
     Response.Headers.Add("X-Content-Type-Options", "nosniff"); 

     return File(System.IO.File.ReadAllBytes(file), "application/pdf"); 
} 
+0

Ashleyありがとう! –

+6

これを動作させるのに苦労している人のためのクイックチェック: 'FileStreamResult'を構築するときに' fileDownloadName'パラメータを渡さないようにしてください。そうしないと、カスタム 'Content-Disposition'ヘッダが上書きされます。 –

5

、あなたは最後のパラメータが示すFile(Stream, string, string)オーバーロードを使用しますか、名前のファイルをダウンロードするために提示される下:

return File(stream, "content/type", "FileDownloadName.ext"); 

それともなどFileStreamResultとして、ストリーミングをサポートし、既存の応答タイプを活用し、コンテンツ・処分を自分で設定することができます。 FileResultExecutorBaseに示されているように、これを行うための標準的な方法は、単にあなたのアクションメソッドでは、応答にヘッダーを自分で設定することです:File()として

// Set up the content-disposition header with proper encoding of the filename 
var contentDisposition = new ContentDispositionHeaderValue("attachment"); 
contentDisposition.SetHttpFileName("FileDownloadName.ext"); 
Response.Headers[HeaderNames.ContentDisposition] = contentDisposition.ToString(); 

// Return the actual filestream 
return new FileStreamResult(@"path\to\file", "content/type"); 
1

Content-Dispositionを無視し、私はこれを使用:

Response.Headers[HeaderNames.ContentDisposition] = new MimeKit.ContentDisposition { FileName = fileName, Disposition = MimeKit.ContentDisposition.Inline }.ToString(); 
return new FileContentResult(System.IO.File.ReadAllBytes(filePath), "application/pdf"); 

これは動作します:-)

関連する問題