【发布时间】:2016-05-01 04:38:34
【问题描述】:
我是 MVC 新手,正在尝试构建一个页面以将多个图像上传到我的站点。但是,Request.Files 集合包含字符串,所有字符串都包含“FileUpload”。我已经阅读了许多从该站点上传的多个文件,但似乎都没有工作。我不确定我哪里出错了,希望有经验的眼睛可以提供帮助。一旦我可以获得正确的文件信息,我就可以上传到我的网站。
感谢您的帮助, 艾伦
源文件包含在下面。
ImagesController.cs
using System;
using System.Collections.Generic;
using System.Linq;
using System.Web;
using System.Web.Mvc;
namespace MyApp.Controllers
{
public class ImagesController : Controller
{
// GET: Images/Upload
[HttpGet]
public ActionResult Upload()
{
return View();
}
// POST: Images/Upload
[HttpPost]
public ActionResult Upload(FormCollection formCollection)
{
try
{
HttpFileCollectionBase files = Request.Files;
foreach(HttpPostedFileBase file in files)
{
int length = file.ContentLength;
string type = file.ContentType;
string filename = file.FileName;
}
return RedirectToAction("Upload");
}
catch
{
return View();
}
}
}
}
上传.cshtml
@{Layout = null;}
<!DOCTYPE html>
<html>
<head>
<meta name="viewport" content="width=device-width" />
<title>Upload</title>
</head>
<body>
<form method="post" action="~/Images/Upload" id="form1" enctype="multipart/form-data">
<table style="margin:1px; background-color:#ccc;">
<tr style="background-color:#fff;">
<td><h1>File Upload Form</h1></td>
<td rowspan="3">
@if (ViewData.Model != null)
{
foreach (var item in ViewData.Model)
{
<img src="/Images/@item["Path"]" alt="FileUpload Image" />
}
}
</td>
</tr>
<tr style="background-color:#fff;">
<td>
<input type="file" name="FileUpload" id="FileUpload" multiple="multiple" />
</td>
</tr>
<tr style="background-color:#fff;">
<td>
<input type="submit" name="btnSubmit" value="Upload" id="btnSubmit" />
</td>
</tr>
</table>
</form>
</body>
</html>
【问题讨论】:
标签: c# image file-upload asp.net-mvc-5