Best way to save image?

I have an asp.net mvc application that allows me to upload images. I am wondering what is the best way to do this.

I have it now

HttpPostedFileBase uploadedImg = Session[SessionImgKey] as HttpPostedFileBase; if (uploadedImg != null) { string fileName = CreateFile(MyField.Name, uploadedImg); tableA.ImagePath = String.Concat(ImgFolderPathLoctaion, "\\", fileName); } 

This is good, but I want to transfer it to my service level, and I do not want to import the web.dll file into my service level project.

So should I use stream? or something like Save Image (I think it might be more focused on images through the paint class, rather than on uploaded images.

0
source share
2 answers

You can get the stream of images from the published file, convert it to an image (System.Drawing) and then save it:

 var stream = uploadedImg.InputStream; var buffer = new byte[stream.Length]; stream.Read(buffer, 0, buffer.Length); //convert stream into image Image myImage = Image.FromStream(new MemoryStream(buffer)); myImage.Save("c:\myimage.jpg"); 
+1
source

If you need to transfer the file to the service level, use the InputStream property of the HttpPostedFileBase property to transfer the stream. By doing this in this way, your service level does not know your web level / presentation.

0
source

Source: https://habr.com/ru/post/1388032/


All Articles