Asp.net mvc asp.net mvc 4和jquery文件上载请求.Files.Count始终为0

Asp.net mvc asp.net mvc 4和jquery文件上载请求.Files.Count始终为0,asp.net-mvc,jquery-file-upload,Asp.net Mvc,Jquery File Upload,我目前正在使用asp.net mvc 4,并使用jquery文件上载上载图像,如果我这样初始化: $('#fileupload').fileupload(); $('#fileupload').fileupload('option', { //url: '/Admin/News/Create', maxFileSize: 500000000, acceptFileTypes: /(\.|\/

我目前正在使用asp.net mvc 4,并使用jquery文件上载上载图像,如果我这样初始化:

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

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

        $('#fileupload').fileupload('option', {
            //url: '/Admin/News/Create',
            maxFileSize: 500000000,
            acceptFileTypes: /(\.|\/)(gif|jpe?g|png)$/i,
            maxNumberOfFiles: 1,
            resizeMaxWidth: 1920,
            resizeMaxHeight: 1200,
        });
选择图像文件时,图像可以在borwser中预览,但在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,
        });
        //$('#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);
            }
        }           
    }

我也遇到了同样的问题,解决方法如下:

    [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");  

   }

希望这能有所帮助。

为什么这行
url:'/Admin/News/Create',
被注释掉了?因为我不想自动上传,而且我使用的是强类型视图,表单有它的操作assigned@BehnamEsmaili请查看问题Bitmap bmp=新位图(Bitmap.FromStream(InputStream));我尝试过其他一些方法,例如使用图像而不是位图,这一行始终命中异常“参数无效”您是指Request.InputStream为null吗?如果字节有值,则
Request.InputStream
也必须有值。当使用Image.FromStream(stream)Bitmap时,FromStream(stream)将命中该异常