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

99封情书 提交于 2019-12-24 14:06:06

问题


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.

来源:https://stackoverflow.com/questions/30889838/image-loss-quality-after-upload-via-httppostedfile-saveasimgpath

易学教程内所有资源均来自网络或用户发布的内容,如有违反法律规定的内容欢迎反馈
该文章没有解决你所遇到的问题?点击提问,说说你的问题,让更多的人一起探讨吧!