Image loss quality after upload via HttpPostedFile.SaveAs(imgPath)

162 views Asked by At

I'm working in a project that doesn't use much of the .net framework and instead work with Request.Form posts.

The code that was being used to save the images (this work, but loss quality) is this one:

files[0].SaveAs(HttpContext.Current.Server.MapPath("../uploads/") + filename);

So I tried to change to this, but now it doesn't even save the image. (the uploadStream.Read(buffer, 0, buffer.Length) comes with the value of 0.

string imagePath = HttpContext.Current.Server.MapPath("../uploads/") + filename;
using (BinaryReader uploadStream = new BinaryReader(files[0].InputStream))
{
    using (FileStream fileStream = new FileStream(imagePath, FileMode.Create))
    {
        byte[] buffer = new byte[32768];
        int read;
        while ((read = uploadStream.Read(buffer, 0, buffer.Length)) > 0)
        {
            fileStream.Write(buffer, 0, read);
        }
    }
}

I built this code basing myself on the question/answers of Image upload without loss of quality. I'm using .net 4.0 on the application.


Is there another way, or what I was doing is the right way and I just missed something? PS: I don't get any error and on the database, I save the correct information, but I don't get the file on the folder. The upload to that folder works, since it work with the SaveAs() method.

PS: I may be wrong, about SaveAs being the reason to loss of quality on the image, but the files[0] come right from the HttpFileCollection and the upload is only there.

0

There are 0 answers