如何在mvc4中创建ImageOptimize(获取错误)?

时间:2012-10-15 15:18:07

标签: image image-processing file-upload asp.net-mvc-4

大家好我在mvc4工作时编写图像优化内容的代码显示错误我需要这样做任何正文plz帮我解决这个问题得到这个错误

这是我的代码:控制器

 public ActionResult Uploading(ImageModel model)
        {
            var uploadFolder = HostingEnvironment.MapPath("~/App_Data");
            uploadFolder = Path.Combine(uploadFolder, DateTime.Now.ToString("yyyy/MM/dd/hh/mm/ss/fff"));
            Directory.CreateDirectory(uploadFolder);
            var streamProvider = new PreserveFilenameMultipartFileStreamProvider(uploadFolder);

             Request.Content.ReadAsMultipartAsync(streamProvider).ContinueWith(t =>
            {
                foreach (var uploadedFile in Directory.GetFiles(uploadFolder))
                {

                    var medium = Path.Combine(uploadFolder, "medium-" + Path.GetFileName(uploadedFile));
                    var thumbnail = Path.Combine(uploadFolder, "thumb-" + Path.GetFileName(uploadedFile));
                    ImageTools.Resize(uploadedFile, thumbnail, 100, 100);
                    ImageTools.Resize(uploadedFile, medium, 50, 50);
                }
                return new HttpResponseMessage()
                {
                    Content = new StringContent("File uploaded.")

                };

            });
           return View("Upload", model);

        } 

这是我的索引页面:

@using (Html.BeginForm("Uploading", "Upload", FormMethod.Post, new { enctype = "multipart/form-data" }))
 {
     <input type="file" name="ImageUploaded" id="btnUpload" multiple="multiple" accept="image/*"  />
 <button type="submit"  id="Upload">Upload</button>
     <br />
     //@Html.DisplayForModel(@ViewData["Time"]);        
     <label>@ViewData["Time"]</label>

 }

这里我在内容中收到错误:HTTPRequestBase不包含内容的定义

       Request.Content.ReadAsMultipartAsync(streamProvider).ContinueWith(t =>

可以提前帮助我解决问题

1 个答案:

答案 0 :(得分:-1)

请在您的视图中使用以下行:

@using (Html.BeginForm("Action Name", "Controller Name", FormMethod.Post, new { enctype = "multipart/form-data" }))

由于您要上传文件,因此您需要定义上面的代码行。