使用流与文件上传某种程度上操纵的结果 [英] Using stream with fileupload manipulating the result somehow

查看:107
本文介绍了使用流与文件上传某种程度上操纵的结果的处理方法,对大家解决问题具有一定的参考价值,需要的朋友们下面随着小编来一起学习吧!

问题描述

突然被显示之前停止显示在页面上,并检查该文件夹的图片*发现当我点击这些图像不显示。调试我的code,我发现注释行是导致它。是code低于反正操纵流?

Suddenly pictures that was showing before stopped showing on the page and checking the folder * discovered the image don't show when I click on them. Debugging my code I found the commented lines were causing it. Was the code below manipulating the stream in anyway?

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。是使用流,以确定上载的图像的尺寸。

The saved files had the right image extension e.g. JPG. Am using the stream to determine the dimension of the uploaded image.

注释的部分是什么原因造成了这个问题。我怎样才能做到这一点的输入流仍然完好无损?

The commented portion is what is causing this issue. How can I do this with the Input stream still intact?

感谢。

推荐答案

现在的问题是这一行:

 return View(photoFileModel);

当code命中此行退出了方法。因此,不执行该行:

When the code hits this line it exits out of the method. Therefore, this line is not executed:

photoFile.SaveAs(fileDestName);  

这篇关于使用流与文件上传某种程度上操纵的结果的文章就介绍到这了,希望我们推荐的答案对大家有所帮助,也希望大家多多支持IT屋!

查看全文
相关文章
登录 关闭
扫码关注1秒登录
发送“验证码”获取 | 15天全站免登陆