2016-11-04 8 views
2

enter image description here助けてください。 「PDF文書の読み込みに失敗しました」と表示されます。PDFドキュメント(MVC)を読み込めません

私はコードで間違った箇所を見つけられませんでした。

これは私のコードです:

ビュー:

<a data-toggle="tooltip" data-placement="top" title="View" href="@Url.Action("DownloadFile", new { id = pat.ID })" target="_blank" class=" btn btn-success btn-sm"> 
<span class="glyphicon glyphicon-file" aria-hidden="true"></span></a> 

はコントローラー:

public FileStreamResult DownloadFile(int id) 
{ 
    MemoryStream workStream = new MemoryStream(); 
    DataModel DB = new DataModel(); 
    /var content = DB._PATIENT.Where(m => m.ID == id).FirstOrDefault(); 
    byte[] contents = (byte[])content.Result; 
    workStream.Write(contents, 0, contents.Length); 
    workStream.Position = 0; 

    Response.AddHeader("Content-Disposition", "inline; filename=someFile.pdf"); 
    return new FileStreamResult(workStream, "application/pdf");  
} 

モデル:

public int ID { get; set; } 
public string PatientCode { get; set; } 
public string CaseNo { get; set; } 
public DateTime DatePerformed { get; set; } 
public byte[] Result { get; set; } 
public DateTime ExpirationDate { get; set; } 
public string LaboratoryName { get; set; } 
+0

エラーメッセージを投稿してください。 –

+0

これは、ここで返される有効なpdfバイトですか? '[] [] contents =(byte [])content.Result; – Aruna

+0

@Arunaはい、有効です。 –

答えて

0

は、単にバイト配列を返します。

public ActionResult DownloadFile(int id) { 
    var DB = new DataModel(); 
    var patient = DB._PATIENT.Where(m => m.ID == id).FirstOrDefault(); 
    if (patient != null && patient.Result != null && patient.Result.Length > 0) { 
     var content = patient.Result; //this is a byte[] of the pdf 
     Response.AddHeader("Content-Disposition", "inline; filename=someFile.pdf"); 
     return File(content, "application/pdf"); 
    } 
    return RedirectToAction("BadPatientFile"); 
} 
+0

ご回答ありがとうございます。引き続きエラーが発生しました:「PDFドキュメントを読み込めませんでした」 –

+0

バイト配列にデータが破損している可能性があります。 – Nkosi

+0

ブラウザ固有のものではないことを他のブラウザでも確認してください。 – Nkosi

0

はあなたがFileContentResult代わりのを返すことができます以下のように、content.Resultが既にPDFのバイト配列であることを考えると

public ActionResult DownloadFile(int id) 
{ 
    MemoryStream workStream = new MemoryStream(); 
    DataModel DB = new DataModel(); 
    var content = DB._PATIENT.Where(m => m.ID == id).FirstOrDefault(); 
    byte[] contents = (byte[])content.Result; 
    workStream.Write(contents, 0, contents.Length); 
    workStream.Position = 0; 

    Response.AddHeader("Content-Disposition", "inline; filename=someFile.pdf"); 
    return File(workStream, "application/pdf", "someFile.pdf"); 
} 
+0

こんにちは、私は次のエラーが表示されます: 'System.Web.Mvc.RedirectToRouteResult'を 'System.Web.Mvc.FileContentResult'に暗黙的に変換できません。 –

+0

戻り値の型を 'ActionResult'として試してください。上記のコードを編集しました。 – Aruna

+0

お返事ありがとうございます。このエラーが発生しました:localhostページが動作していません localhostが無効な応答を送信しました。 ERR_RESPONSE_HEADERS_MULTIPLE_CONTENT_DISPOSITION –

関連する問題