使用带有fileupload的流以某种方式操纵结果

时间:2012-10-06 17:15:07

标签: c# .net asp.net-mvc-3 image-processing file-upload

突然显示在页面停止显示之前显示的图片并检查文件夹*发现图像在我点击它们时没有显示。调试我的代码我发现注释行引起了它。以下代码是否操纵了流?

public ActionResult UploadPhoto(UserPhotoUploadModel photoFileModel)
{
        HttpPostedFileBase photoFile = photoFileModel.PhotoFile;
    string uploadPath = Server.MapPath("~/Content/users/photos");
    string autoFileName = String.Format("{0}_{1}", Guid.NewGuid().ToString(), filename);
    string fileDestName = Path.Combine(uploadPath, autoFileName);

    //Starting from here is where the issue lies
    Stream imgStream = photoFile.InputStream;
    using (imgStream)
    {
        Image img = Bitmap.FromStream(imgStream);
        int width = img.Width;
        int height = img.Height;

        if (width > 100 || height > 100)
        {
            ModelState.AddModelError("PhotoFile", "Photo dimension should be 100 by 100 or less");
            return View(photoFileModel);
        }
    }
    //the issue ends here

    photoFile.SaveAs(fileDestName);

    return RedirectToAction("Profile", new {id = loggedinUser.ProviderUserKey});
}

保存的文件具有正确的图片扩展名,例如JPG。我使用流来确定上传图像的尺寸。

评论部分是导致此问题的原因。如何在输入流仍然完整的情况下执行此操作?

感谢。

1 个答案:

答案 0 :(得分:0)

问题在于这一行:

 return View(photoFileModel);

当代码命中此行时,它退出方法。因此,此行不会执行:

photoFile.SaveAs(fileDestName);