2016-05-29 64 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