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 アクション Request.Files.Count では、ファイルがアップロードされていないことを意味します。そして、次のように初期化すると:

        //$('#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 に答える