私は現在 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);
}
}
}