【问题标题】:dynamically displaying PDFs in C# MVC with iframe使用 iframe 在 C# MVC 中动态显示 PDF
【发布时间】:2016-05-29 20:47:26
【问题描述】:

我有一个文件按钮来搜索文件。我想采用选定的路径并将其显示在 iframe 中。

<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 更新为我使用编辑器字段选择的 Pdf 吗?

【问题讨论】:

  • 你想要达到什么目的? PDF 位于客户端计算机上还是服务器上?我的假设是您正在尝试从客户端计算机显示 PDF 的预览?如果是这样,您的解决方案将不起作用,因为 GetPDF 操作将尝试从服务器端加载 PDF。

标签: c# asp.net-mvc pdf iframe razor


【解决方案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();
}

【讨论】:

  • thx,但这对我帮助不大,因为我无法获得我的路径:
    @Html.TextBox("file", "", new {类型 = "文件" })
猜你喜欢
  • 1970-01-01
  • 2016-09-27
  • 1970-01-01
  • 1970-01-01
  • 1970-01-01
  • 2019-05-07
  • 1970-01-01
  • 2012-03-31
  • 2017-11-13
相关资源
最近更新 更多