2016-05-29 62 views
1

我有一個文件按鈕來搜索文件。我想要選擇的路徑並將其顯示在iframe中。在iframe中動態顯示C#MVC中的PDF

<div class="editor-field"> 
      @Html.TextBox("file", "", new { type = "file" }) 
</div> 

我現在的iframe是:

@Html.AntiForgeryToken() 
    <iframe src="@Url.Action("GetPDF")" ; height="1000" ; width="1000";%>'></iframe> 

我現在的(靜態)GetPDF方法如下:

public FileStreamResult GetPDF() 
{ 

     FileStream fs = new FileStream("D:\\Temp.pdf", FileMode.Open, FileAccess.Read); 
     return File(fs, "application/pdf"); 

} 

所以,請你幫幫我,告訴我我該怎麼更新我的Iframe與我選擇的編輯字段一致嗎?

+0

什麼是你想實現的呢? PDF位於客戶端機器上還是服務器上?我的假設是你試圖從客戶端機器上顯示PDF預覽?如果是這樣,你的解決方案將無法工作,因爲'GetPDF'操作將嘗試從服務器端加載PDF。 –

+0

來自客戶端。 – Lukas

回答

1

我相信已經有一個回答你的問題,它是如下: Display PDF in iframe

編輯1:

[HttpPost] 
    public ActionResult GetPdf(HttpPostedFileBase uploadedPdf) 
    { 
     // user has selected a file 
     if (uploadedPdf!= null && uploadedPdf.ContentLength > 0) 
     { 
      //you have the file stream here *uploadedPdf* 

      return File(fs, "application/pdf"); 
     } 

     return null;  
    } 

爲了實現異步文件上傳你可以看看 thisjQueryForm並在文件輸入上附加事件。

編輯2: 簡單的方法來從流獲取到字節數組

using (MemoryStream ms = new MemoryStream()) { 
    file.InputStream.CopyTo(ms); 
    byte[] array = ms.GetBuffer(); 
} 
+0

thx,但這並不能幫助我很多,因爲我無法得到我的路徑:

@Html.TextBox("file", "", new { type = "file" })
Lukas