为什么文件上传没有绑定到我的视图模型?
Why aren't file uploads binding to my viewmodel?
我正在尝试将文件上传绑定到 ViewModel(如 this post 中所示)。
但是我无法让文件绑定到 ViewModel 上的 Files
属性。
请看下面的代码。我做错了什么?
(为清楚起见进行编辑 - 我希望上传绑定到虚拟机,而不是将它们作为操作参数。)
ViewModel
public class PrimaryImageUploadViewModel
{
public PrimaryImageUploadViewModel()
{
}
public HttpPostedFileBase[] Files { get; set; }
public string Title { get; set; }
}
动作
[HttpPost]
public async Task<ActionResult> Upload(PrimaryImageUploadViewModel postedModel)
{
var requestFiles = postedModel.Files; // THIS VALUE IS NULL - WHY?
foreach (var f in requestFiles)
{
if (f.ContentLength == 0)
{
// do stuff
}
}
}
请求Headers
Accept:*/*
Content-Disposition:attachment; filename="MyImage.png"
Content-Type:multipart/form-data; boundary=----WebKitFormBoundaryEj8zSF9hwGU3ZQA9
Origin:http://localhost:52588
Referer:http://localhost:52588/example/edit/1
User-Agent:Mozilla/5.0 (Windows NT 10.0; Win64; x64) AppleWebKit/537.36 (KHTML, like Gecko) Chrome/63.0.3239.132 Safari/537.36
X-Requested-With:XMLHttpRequest
请求负载
------WebKitFormBoundaryEj8zSF9hwGU3ZQA9
Content-Disposition: form-data; name="Title"
Some Test Title
------WebKitFormBoundaryEj8zSF9hwGU3ZQA9
Content-Disposition: form-data; name="files[]"; filename="MyImage.png"
Content-Type: image/png
------WebKitFormBoundaryEj8zSF9hwGU3ZQA9--
首先,如果您希望能够上传文件,请确保已将表单上的 enctype attribute
设置为 multipart/form-data
:
您可以在 post 操作参数中访问上传的文件,
[HttpPost]
public async Task<ActionResult> Upload(PrimaryImageUploadViewModel postedModel, HttpPostedFileBase file)
{
if (file.ContentLength > 0)
{
string _FileName = Path.GetFileName(file.FileName);
string _path = Path.Combine(Server.MapPath("~/UploadedFilesFolder"), _FileName);
file.SaveAs(_path);
}
}
public class Model1
{
public HttpPostedFileBase[] Files { get; set; }
public string Title { get; set; }
}
希望这对您有所帮助...
我正在尝试将文件上传绑定到 ViewModel(如 this post 中所示)。
但是我无法让文件绑定到 ViewModel 上的 Files
属性。
请看下面的代码。我做错了什么?
(为清楚起见进行编辑 - 我希望上传绑定到虚拟机,而不是将它们作为操作参数。)
ViewModel
public class PrimaryImageUploadViewModel
{
public PrimaryImageUploadViewModel()
{
}
public HttpPostedFileBase[] Files { get; set; }
public string Title { get; set; }
}
动作
[HttpPost]
public async Task<ActionResult> Upload(PrimaryImageUploadViewModel postedModel)
{
var requestFiles = postedModel.Files; // THIS VALUE IS NULL - WHY?
foreach (var f in requestFiles)
{
if (f.ContentLength == 0)
{
// do stuff
}
}
}
请求Headers
Accept:*/*
Content-Disposition:attachment; filename="MyImage.png"
Content-Type:multipart/form-data; boundary=----WebKitFormBoundaryEj8zSF9hwGU3ZQA9
Origin:http://localhost:52588
Referer:http://localhost:52588/example/edit/1
User-Agent:Mozilla/5.0 (Windows NT 10.0; Win64; x64) AppleWebKit/537.36 (KHTML, like Gecko) Chrome/63.0.3239.132 Safari/537.36
X-Requested-With:XMLHttpRequest
请求负载
------WebKitFormBoundaryEj8zSF9hwGU3ZQA9
Content-Disposition: form-data; name="Title"
Some Test Title
------WebKitFormBoundaryEj8zSF9hwGU3ZQA9
Content-Disposition: form-data; name="files[]"; filename="MyImage.png"
Content-Type: image/png
------WebKitFormBoundaryEj8zSF9hwGU3ZQA9--
首先,如果您希望能够上传文件,请确保已将表单上的 enctype attribute
设置为 multipart/form-data
:
您可以在 post 操作参数中访问上传的文件,
[HttpPost]
public async Task<ActionResult> Upload(PrimaryImageUploadViewModel postedModel, HttpPostedFileBase file)
{
if (file.ContentLength > 0)
{
string _FileName = Path.GetFileName(file.FileName);
string _path = Path.Combine(Server.MapPath("~/UploadedFilesFolder"), _FileName);
file.SaveAs(_path);
}
}
public class Model1
{
public HttpPostedFileBase[] Files { get; set; }
public string Title { get; set; }
}
希望这对您有所帮助...