通过HttpPostedFile.SaveAs(imgPath)上传后的图像丢失质量

时间:2015-06-17 11:09:45

标签: c# image image-uploading httpfilecollection

我正在开发一个不使用.net框架的项目,而是使用Request.Form个帖子。

用于保存图像的代码(此工作,但损失质量)是这样的:

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

所以我试图改变这个,但现在它甚至没有保存图像。 (uploadStream.Read(buffer, 0, buffer.Length)的值为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);
        }
    }
}

我根据Image upload without loss of quality的问题/答案建立了这段代码。我在应用程序上使用.net 4.0

还有另一种方式,或者我正在做的是正确的方式而我错过了什么? PS:我没有得到任何错误,在数据库中,我保存了正确的信息,但我没有得到该文件夹​​上的文件。上传到该文件夹​​的工作正常,因为它适用于SaveAs()方法。

PS:我可能错了,关于SaveAs是造成图像质量下降的原因,但files[0]来自HttpFileCollection并且上传仅在那里。

0 个答案:

没有答案