6

我目前使用 asp.net mvc 4 ,并使用 jquery-file-upload 上传图片,如果我这样初始化:

        $('#fileupload').fileupload();

        $('#fileupload').fileupload('option', {
            //url: '/Admin/News/Create',
            maxFileSize: 500000000,
            acceptFileTypes: /(\.|\/)(gif|jpe?g|png)$/i,
            maxNumberOfFiles: 1,
            resizeMaxWidth: 1920,
            resizeMaxHeight: 1200,
        });

选择图片文件时,可以在浏览器中预览图片,但在mvc Action Request.Files.Count中为0,表示没有上传文件。如果我这样初始化:

        //$('#fileupload').fileupload();

        $('#fileupload').fileupload('option', {
            //url: '/Admin/News/Create',
            maxFileSize: 500000000,
            acceptFileTypes: /(\.|\/)(gif|jpe?g|png)$/i,
            maxNumberOfFiles: 1,
            resizeMaxWidth: 1920,
            resizeMaxHeight: 1200,
        });

我无法预览图像,但 mvc 操作获取文件,有人知道为什么吗?控制器的邮政编码:

    [HttpPost]
    [ValidateInput(false)]
    public ActionResult Create(NewsViewModel model, FormCollection form)
    {
        if (ModelState.IsValid)
        {
            //....

            // upload image
            foreach (string file in Request.Files)
            {
                HttpPostedFileBase hpf = Request.Files[file] as HttpPostedFileBase;
                if (hpf.ContentLength == 0)
                    continue;
                string path = Path.Combine(Server.MapPath("~/Uploads/News/"),GUID.NewGuid()+ Path.GetExtension(hpf.FileName));
                hpf.SaveAs(path);

                data.ImagePath = path;
                _iNewsService.UpdateNews(data);
            }
        }           
    }
4

1 回答 1

0

我有同样的问题,解决了以下问题:

    [HttpPost]
    [ValidateInput(false)]
    public ActionResult Create(NewsViewModel model, FormCollection form)
    {                  
            var length = Request.ContentLength;
            var bytes = new byte[length];
            Request.InputStream.Read(bytes, 0, length); 

            //or for creating image from stream 

            Bitmap bmp = new Bitmap(Bitmap.FromStream(InputStream));
            bmp.Save("some path");  

   }

希望这会有所帮助。

于 2013-01-01T09:28:35.163 回答