我想上传几个文件。上传本身适用于单个上传,但我无法弄清楚如何访问元素名称以确保每次上传都分配到了正确的字段。 HttpPostedFileBase似乎不再包含该类型的信息。在服务器站点上传多个文件/访问输入ID
public ActionResult Edit(int id, FormCollection collection) {
Report report = re.GetReport(id);
var fileNames = new List<string>();
foreach (string file in Request.Files) {
var postedFile = Request.Files[file] as HttpPostedFileBase;
if (postedFile.ContentLength == 0)
continue;
fileNames.Add(UploadFile(basedir, postedFile));
}
// Rather than guessing which is which I'd like to get the field name or id.
report.Image = fileNames[0];
report.File = fileNames[1];
UpdateModel(report, "report");
rep.Save();
在视图中我有
<%: Html.LabelFor(model => model.report.Image)%>
<input id="report_Image" type="file" name="Image" />
<%: Html.LabelFor(model => model.report.File)%>
<input id="report_Image" type="file" name="File" />
感谢, 达菲
菲尔的帖子其实演示了此问题。他会保存一个或两个文件,但无法分辨哪个文件来自哪个文件夹。 查看扩展在我简化的例子中工作,但两个上传都是相同类型时失败。 (例如,一个图像用于页眉,一个用于页脚。 – duffy 2010-08-04 06:09:38