我需要一次上传选定的多张图片,并使用razor在asp .net mvc 4中显示每个图像的进度条。请任意一个帮助
答案 0 :(得分:0)
这是基本的:
这个例子不言自明。希望这有帮助!
创建用于上传多个文件的表单:
<form action="" method="post" enctype="multipart/form-data">
<label for="lblFile1">Filename:</label>
<input type="file" name="files" id="file0" />
<label for="file2">Filename:</label>
<input type="file" name="files" id="file1" />
<input type="submit" value="Upload"/>
</form>
现在在您的控制器发布操作中:
[HttpPost]
public ActionResult Upload(IEnumerable<HttpPostedFileBase> files) {
foreach (var file in files) {
if (file.ContentLength > 0) {
var fileName = Path.GetFileName(file.FileName);
var path = Path.Combine(Server.MapPath("~/App_Data/Uploads"), fileName);
file.SaveAs(path);
}
}
return RedirectToAction("Upload");
}
编辑:
我没有太多时间创建带有进度条的示例项目..但是这个link肯定会对您有所帮助!
答案 1 :(得分:0)
namespace ImageUploading.Controllers
{
public class HomeController : Controller
{
public ActionResult Index()
{
return View();
}
[HttpPost]
public ActionResult Index(HttpPostedFileBase file)
{
try
{
string fileName = System.IO.Path.GetFileName(file.FileName);
file.SaveAs(Server.MapPath("~/Images/" + fileName));
string filePath = "Images/" + fileName;
ViewBag.message = "Image Uploaded Succesfully ";
}
catch
{
ViewBag.message = "Image Uploading Failed";
}
return View();
}
}
}