2017-07-13 3 views
1

クロムで開いたpdfファイルを取得しようとしましたが、表示中に途中で止まってしまったようです。 PDF表示がブラウザが正しく見えるインライン作るためにCHROME&FireBoxを使用してファイルをダウンロードするのではなく、ブラウザでファイルを開くようにしてください。

public void GetViewFileData(string attachmentID) 
    { 

     List<DownloadFileInfoViewModel> retDownloadFilesInfo = new List<DownloadFileInfoViewModel>(); 
     using (var context = this.GetContext()) 
     { 
      retDownloadFilesInfo = context.GetfileData(attachmentID); 
     } 

     // Clear the content of the response 
     HttpContext.Current.Response.Clear(); 
     HttpContext.Current.Response.Buffer = true;   
     HttpContext.Current.Response.AddHeader("Content-Disposition", "inline; filename=" + retDownloadFilesInfo[0].FileName);   
     HttpContext.Current.Response.AddHeader("Content-Length", retDownloadFilesInfo[0].FileSize.ToString());   
     HttpContext.Current.Response.ContentType = ReturnExtension(retDownloadFilesInfo[0].FileExt.ToLower()); 
     HttpContext.Current.Response.BinaryWrite(retDownloadFilesInfo[0].FileData); 
     HttpContext.Current.Response.Flush(); // this make stream and without it open 
     HttpContext.Current.ApplicationInstance.CompleteRequest();     
     HttpContext.Current.Response.End(); 
    } 
+0

何ファイルの種類はそれですか? – mrtig

答えて

0

あなたのヘッダー、しかし:

[HttpGet] 
    [Authorize(Roles = ROLE_VIEW)] 
    public void GetViewFiles(string attachmentID) 
    { 
     AttachmentBO bo = new AttachmentBO(this.CurrentUser); 
     bo.GetViewFileData(attachmentID); 

    } 

AttachmentBO.cs:

enter image description here

Screen shot of Request and Result Headers being sent by application serving the PDF

MyControllerを支援してくださいあなたはまたマックする必要がありますMIMEタイプが "application/pdf"として送信されていることを確認してください。

また、ブラウザのユーザー設定である可能性があります。ブラウザでChromeがtest PDFを開いているかどうかをテストしてください。お使いのコンピュータ、オープンChromeで

Open PDFs in Chrome

  • 右上にあるMore Moreをクリックして設定をクリックします。
  • 下部にあるアドバンスをクリックします。
  • 「プライバシーとセキュリティ」で、コンテンツ設定をクリックします。
  • 下部にあるPDFドキュメントをクリックします。
  • オフにするPDFを別のアプリケーションで開く

クリックすると、Chromeが自動的にPDFを開きます。

ファイル名に数字以外の文字が含まれていると、奇妙な動作が見られました。

public void GetViewFileData(string attachmentID) 
{ 

    List<DownloadFileInfoViewModel> retDownloadFilesInfo = new List<DownloadFileInfoViewModel>(); 
    using (var context = this.GetContext()) 
    { 
     retDownloadFilesInfo = context.GetfileData(attachmentID); 
    } 

    HttpResponse response = HttpContext.Current.Response; 
    // Clear the content of the response 
    response.ClearContent(); 
    response.Clear(); 
    response.ContentType = "application/pdf"; 
    string filename = retDownloadFilesInfo[0].FileName; 
    string ext = Path.GetExtension(filename); 
    filename = new string(Path.GetFileNameWithoutExtension(filename).Where(ch => char.IsLetterOrDigit(ch)).ToArray()) + ext; 
    response.AddHeader("Content-Disposition", "inline; filename=" + fileName);   
    response.BinaryWrite(retDownloadFilesInfo[0].FileData); 
    response.Flush(); // this make stream and without it open 
    response.End(); 
} 
+0

別のアプリケーションオプションを使用してPDFを開くことは既にオフになっていますが、まだ機能していません。 – suryakumar101987

+0

正しいMIMEタイプが送信されたことを確認しましたか? –

+0

yes.its "application/pdf"のみ – suryakumar101987

0

Content-Dispositionの値の;filenameセクションでは、それは、ダウンロードのように扱う作っている:

は、ファイル名をクリーニングしてみてください。 Content-Disposition: inlineに設定するだけで、必要に応じて動作します。ファイル名を持つことは、それが添付ファイルであることを意味します。

https://developer.mozilla.org/en-US/docs/Web/HTTP/Headers/Content-Disposition

+0

も同じことを試みましたが、まだ機能していないので、助けてください。 – suryakumar101987

+0

名前を省略すると、pdfファイル名がパスに設定されます(例: 'GetViewFiles.pdf')。名前を設定すると、正しい名前がダウンロードされます。 'Content-Disposition:attachmentと組み合わせて使用​​すると、最終的に' Save As 'ダイアログのデフォルトファイル名として使用されます。これは添付ファイルであることを意味するものではありませんが、ダイアログを保存する – Nico

3

あなただけのシンプルなFileResultを使用して検討しました。

[HttpGet] 
public FileResult GetPdf() 
{ 
    var file = new FileInfo(Server.MapPath("~/App_Data/sample.pdf")); 
    Response.Headers.Add("content-disposition", $"inline; filename={file.Name}"); 

    /* Return the file from a path 
    return File(file.FullName, "application/pdf"); 
    */ 

    //return the file as binary contents 
    var contents = System.IO.File.ReadAllBytes(file.FullName); 
    return File(contents, "application/pdf"); 

} 

FileResultとキーはそれが重複Content-Dispositionヘッダーを設定しますと、ファイル名(第三オプションのパラメータ)を設定しないことです。これにより、PDF表示をサポートするブラウザにPDFが表示されます。

上記の方法は、次のブラウザでテストされています。

  1. クロームバージョン59.0.3071.115
  2. のFirefox 54.0.1
  3. のInternet Explorer 11
  4. マイクロソフトエッジ40.15063.0.0
0

は、ここに私が使用しているものです:

[HttpGet] 
    public FileContentResult GetFile(string path) 
    { 
     VerifyAccessOrThrow(path); 

     var fileName = "example"; 
     //We need to enclose the filename in double quotes for the mighty Firefox 
     Response.AppendHeader("Content-Disposition", $"inline; filename=\"{fileName}\""); 
     Response.ContentType = MimeMapping.GetMimeMapping(fileName); 
     return File(fileContent, MimeMapping.GetMimeMapping(fileName)); 
    } 
+0

mayu、良い仕事の1つは 'FileResult'によって設定された' Response.ContentType'です 'contentType'フィールドは必須です、それを無視すると' ArgumentException'が発生しますので 'Response.ContentType'は必須ではありませんが、ネガティブイマパクトはありません。 – Nico

関連する問題