2

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?

1
  • What is it you're trying to achieve? Where are the PDF's located, on the client machine or on the server? My assumption is you are trying to display a preview of the PDF from the client machine? If so your solution will not work, as the GetPDF action will be trying to load the PDF from server side. Commented May 30, 2016 at 1:03

1 Answer 1

1

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();
}
Sign up to request clarification or add additional context in comments.

1 Comment

thx, but this doesnt help me much, because I am unable to get my path:<div class="editor-field"> @Html.TextBox("file", "", new { type = "file" }) </div>

Your Answer

By clicking “Post Your Answer”, you agree to our terms of service and acknowledge you have read our privacy policy.

Start asking to get answers

Find the answer to your question by asking.

Ask question

Explore related questions

See similar questions with these tags.