【发布时间】:2019-08-09 17:31:00
【问题描述】:
在我的 asp.net 网络应用程序中,我有一个上传文件的部分,它工作正常,但问题是用户看不到进度,当他们想要上传更大的文件时,这尤其非常必要。
所以我的问题是:上传文件时如何显示进度(简单的进度条或百分比)。 我尝试了这个论坛中的一些解决方案,但没有一个对我有用。
这里是控制器:
public ActionResult BriefDetails(int? ID)
{
var dtl = _context.Original.SingleOrDefault(b => b.Id == Id);
var vm = new BriefUploadVM()
{
Id = dtl.Id,
brief_rp = dtl.brief_rp,
};
return View(vm);
}
这是视图模型:
public class BriefUploadVM
{
public int Id { get; set; }
public string BriefReport { get; set; }
[Required(ErrorMessage = "Error: Attach your file")]
[NotMapped]
public HttpPostedFileBase brief_rp { get; set; }
}
这是视图
@using (Html.BeginForm("Store", "Reports", FormMethod.Post, new { enctype = "multipart/form-data" }))
{
<div class="form-group">
<input type="file" name="brief_rp" />
@Html.ValidationMessageFor(a=> a.brief_rp)
</div>
@Html.AntiForgeryToken();
@Html.HiddenFor(a => a.Id);
<button class="btn btn-primary">Submit</button>
}
这是控制器内部的存储方法
[HttpPost]
[ValidateAntiForgeryToken]
public ActionResult Store(BriefUploadVM rp)
{
string Second_filename = Path.GetFileNameWithoutExtension(rp.brief_rp.FileName);
string Second_extension = Path.GetExtension(rp.brief_rp.FileName);
Second_filename = Second_filename + DateTime.Now.ToString("yymmssfff") + Second_extension;
rp.BriefReport = "Files/" + Second_filename;
Second_filename = Path.Combine(Server.MapPath("~/Files/"), Second_filename);
rp.brief_rp.SaveAs(Second_filename);
var item = _context.original.Single(a => a.Id == rp.Id);
item.Brief_Report = rp.BriefReport;
_context.SaveChanges();
return RedirectToAction("Success", "Project");
}
【问题讨论】:
-
“我尝试了这个论坛中的一些解决方案,但没有一个对我有用。”为什么不?您将这个问题与其他问题区分开来的具体要求是什么?
-
"...但是没有一个对我有用..." - 这些信息并没有真正的帮助。它没有说明究竟是什么不起作用,即您是否在任何地方收到任何错误消息,您是否尝试调试问题以确保它确实完成了预期的事情。
-
那些不适用于网络应用程序@JohnB
标签: c# asp.net-mvc