views:

28

answers:

1

So I'm working on a site that is going to need a file upload control where the user would be able to upload an image, and then the page would postback causing the image to appear for their viewing (before they submit the data to the database, the image is being stored as type image). What I do now is have my own private web form where people send me their image, I format it accordingly and use the following simple code to upload it:

byte[] newimage = fileUpImgFile.FileBytes;
var myDataTable = (from item in context.TypeSet where item.Number == txtBxNumber.Text.Trim() select item).ToList();

foreach (Type item in myDataTable)
{
    item.Photo = newimage;
}
context.SaveChanges();

Which works, but in this case, it would only work if the record exists already in the database so the person would have to save the data, then go back in and upload the image (inconvenient and inefficient). Is there a way to upload it, store it in memory, and then display it, without saving it to the database?

A: 

Once you have the uploaded bytes, you can do anything you want with it.

The approach that immediately jumps to mind is storing the bytes in Session for that user.

womp
The site is not using a viewstate or session. How would I go about taking the byte array and setting that to be the source of the image so a postback would cause the image to appear?
dangerisgo
There's a number of ways to do it. You could write a temp file (slow but straightforward), or write a custom HttpHandler that would handle specific requests for these in-memory images, and write the bytes directly to the response output stream. You can google for some relevant examples.
womp