简体   繁体   中英

save files on server side - best practice

in my asp .net application i'm using an entity derived from a data base (a "Product" Entity) . the Entity among-st various fields requires 3 image fields .

i load thous image files using a FileUpload , i would like to add the entity to the context(Entity Framework) after iv'e added the 3 images to it .

  context.AddToProduct(Product);    

i was wondering what would be the best practice of saving these files before submition

ie

i got 3 byte[] arrays that need to be saved for a particular product being uploaded to the site .

i thought of just creating a directory on the server side with the products name , and later when all the product's credentials are submitted ill pack them together with the images into a product entity and save it in the entity model.

i am looking for a more asp .net kinda way to cache them and retrieve them when needed .

any ideas.

here is an example for a single image loaded for the product

           byte[] imageSize = new byte[FileUpload1.PostedFile.ContentLength];
           HttpPostedFile uploadedImage = FileUpload1.PostedFile;
           uploadedImage.InputStream.Read(imageSize,0,(int)FileUpload1.PostedFile.ContentLength);
           MyProduct product = new MyProduct()
           {
               Image = imageSize,
               Date = DateTime.Now,
               State = 1,
               Short = "short discription",
               Long = "long.......................",
               Title = "Some Title",
               Price = (float)11.1,
               OwnerID = int.Parse(Response.Cookies["user"]["id"]),                        
           };    

further more, if i save for Session["file1"] Session["file2"] Session["file3"] but this could cause performance issues if allot of users attempt to load pictures for there files.

If you save in session, only the user who triggered the picture saving will have access to it. All other users will have their Session["file1"] with a different (or null) value as the session is different.

The way I use to do it is to save the images into the database. I like this approach 'cause this way there are no "external dependencies", ie, paths that may be broken.

Saving the image into session will only allow you to cache it for one user. Saving it into a server variable, will allow anyone to retrieve it (just be careful for not to overload the RAM with the images)

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