简体   繁体   中英

dynamically displaying PDFs in C# MVC with iframe

I have a File button to search for a File. I want to take the selected Path and display it in an iframe.

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

My current iframe is:

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

My current (static) GetPDF Method is the following:

public FileStreamResult GetPDF()
{

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

}

So could you please help me and tell me how I can update my Iframe to the Pdf that i choose with my editor-field?

I believe there is already an answer to your question and it is as follows: Display PDF in iframe

EDIT 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;      
    }

In order to achieve async file upload you can look into this or jQueryForm and attach an event on the file input.

EDIT 2: easy way to get from stream to byte array

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

The technical post webpages of this site follow the CC BY-SA 4.0 protocol. If you need to reprint, please indicate the site URL or the original address.Any question please contact:yoyou2525@163.com.

 
粤ICP备18138465号  © 2020-2024 STACKOOM.COM