Is there a way to upload a file, save it to a Stream, this Stream I will save it temporarily in a Session and, at last, I will try to preview this uploaded file that is in this Session??
For example, a pdf file.
Thanks!!
EDITED
Here's what I'm trying to do:
HttpPostedFileBase hpf = Request.Files[0] as HttpPostedFileBase;
byte[] buffer = new byte[hpf.InputStream.Length];
MemoryStream ms = new MemoryStream(buffer);
ms.Read(buffer, 0, (int)ms.Length);
Session["pdf"] = ms.ToArray();
ms.Close();
And in another method, I'm doing this:
byte[] imageByte = null;
imageByte = (byte[])Session["pdf"];
Response.ContentType = "application/pdf";
Response.Buffer = true;
Response.Clear();
Response.BinaryWrite(imageByte);
But nothing happends... my browser even opens a nem page to show the pdf file, but a window is shown saying that the file is not a pdf (or something like the file doesn't initiate with pdf, I didn't understand that)
Sure is. I upload files (PDF/images) to my db in my app. My model object actually stores the file as a byte array but for other functions i have to convert to and from streams so im sure its just as easy to keep it in stream format.
Here are some code examples (copy n paste) from my app-
The
File
object that i use to move files (PDFs / images) around:..the
PdfDoc
type specifically for PDF files:.. an example of an action that receives multi-part POST for file uploading:
Now you can see i pass the file object to my service here but you can choose to add it to the session and pass an id back to the 'preview' view for example.
Finally, here is a generic action i use to render files out to the client (you could have something similar render the files/stream from the Session):
EDIT:
I noticed i need more samples to explain the above-
For the upload action above, the
FileUploadResultDTO
class:And the
GetFilesFromRequest
function:And finally (i hope i have everything you need now) this
ReadFully
function. It's not my design. I got it from the net - stream reading can be tricky. I find this function is the most successful way to completely read a stream:Yes, but you can't save it to a stream. A stream doesn't contain any data, it's just the means for accessing the actual storage.
Get the data as a byte array, then you can put it in a session variable, save it as a file, and send it as a response.
Use a BinaryReader to get the data from the input stream to a byte array:
(Edit: Changed from a StreamReader to a BinaryReader)