MVC3 - 当使用 HttpPostedFileBase 处理大文件时,RedirectToAction 非常慢
好吧,我遇到了一个似乎没有意义的情况。我有一个控制器:
public ActionResult Index()
{
return View(_courseService.ListAllCourses());
}
[HttpPost]
public ActionResult CreateNewCourse(CourseVDO course, HttpPostedFileBase CourseDataFile)
{
return RedirectToAction("Index");
}
和一个视图:
@using (Html.BeginForm("CreateNewCourse", "Home", FormMethod.Post, new { enctype = "multipart/form-data" }))
{
@Html.ValidationSummary(false)
<fieldset>
<legend>Course</legend>
<div class="editor-label">
@Html.LabelFor(model => model.Name)
</div>
<div class="editor-field">
@Html.EditorFor(model => model.Name)
@Html.ValidationMessageFor(model => model.Name)
</div>
<div class="editor-label">
Course Data File
</div>
<div class="editor-field">
<input type="file" name="CourseDataFile" id="CourseDataFile" />
</div>
<div class="editor-label">
@Html.LabelFor(model => model.Visible)
</div>
<div class="editor-field">
@Html.EditorFor(model => model.Visible)
@Html.ValidationMessageFor(model => model.Visible)
</div>
<p>
<input type="submit" value="Create" />
</p>
</fieldset>
}
当我提交一个大约 200KB 的文件时,它会足够快地上传到服务器(毕竟它是本地的),但从“return RedirectToAction("Index"”开始需要 5 秒); " 回到“return View(_courseService.ListAllCourses());”上的断点行(实际上并未执行 ListAllCourses)。这意味着它完全取决于内部管道。更糟糕的是,这种延迟会随着文件大小而变化。到底发生了什么事,我该如何阻止它?
谢谢
Okay, I have a situation that seems to make no sense. I have a controller thus:
public ActionResult Index()
{
return View(_courseService.ListAllCourses());
}
[HttpPost]
public ActionResult CreateNewCourse(CourseVDO course, HttpPostedFileBase CourseDataFile)
{
return RedirectToAction("Index");
}
And a View thus:
@using (Html.BeginForm("CreateNewCourse", "Home", FormMethod.Post, new { enctype = "multipart/form-data" }))
{
@Html.ValidationSummary(false)
<fieldset>
<legend>Course</legend>
<div class="editor-label">
@Html.LabelFor(model => model.Name)
</div>
<div class="editor-field">
@Html.EditorFor(model => model.Name)
@Html.ValidationMessageFor(model => model.Name)
</div>
<div class="editor-label">
Course Data File
</div>
<div class="editor-field">
<input type="file" name="CourseDataFile" id="CourseDataFile" />
</div>
<div class="editor-label">
@Html.LabelFor(model => model.Visible)
</div>
<div class="editor-field">
@Html.EditorFor(model => model.Visible)
@Html.ValidationMessageFor(model => model.Visible)
</div>
<p>
<input type="submit" value="Create" />
</p>
</fieldset>
}
When I submit a file of around 200KB, it uploads to the server fast enough (it's local after all), but then takes 5 seconds to go from the "return RedirectToAction("Index"); " line back to the breakpoint on the "return View(_courseService.ListAllCourses());" line (not actually executing the ListAllCourses). This means it's entirely down to the internal plumbing. Worse, this delay scales with file size. What on earth is going on, and how can I stop it?
Thanks
如果你对这篇内容有疑问,欢迎到本站社区发帖提问 参与讨论,获取更多帮助,或者扫码二维码加入 Web 技术交流群。
绑定邮箱获取回复消息
由于您还没有绑定你的真实邮箱,如果其他用户或者作者回复了您的评论,将不能在第一时间通知您!
发布评论
评论(1)
我以前从未使用过该方法,这不是直接答案,但也许它是一个更好的解决方案:
我使用模型,因此在我的模型项中我使用 UIHint("uploadbox")
这是views/Shared/EditorTemplates/内的代码 这里的UploadField.cshtml
是上传功能的使用示例:
这是编辑项目时的代码,以防文件没有更改,但其他项目
只是一个想法:-)
I have never used that method before, and this is no direct answer, but maybe its a better solution:
I use models so in my model item i use the UIHint("uploadbox")
here is the code inside views/Shared/EditorTemplates/UploadField.cshtml
here is an example of the usage of the upload feature:
here is the code when editing the item, in case the file was not changed, but others items have
just a thought :-)