Reputation: 77
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?
Upvotes: 2
Views: 4302
Reputation: 1339
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();
}
Upvotes: 1