MVC3 - 当使用 HttpPostedFileBase 处理大文件时,RedirectToAction 非常慢

发布于 2025-01-02 22:06:41 字数 1786 浏览 0 评论 0原文

好吧,我遇到了一个似乎没有意义的情况。我有一个控制器:

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 技术交流群。

扫码二维码加入Web技术交流群

发布评论

需要 登录 才能够评论, 你可以免费 注册 一个本站的账号。

评论(1

伴梦长久 2025-01-09 22:06:41

我以前从未使用过该方法,这不是直接答案,但也许它是一个更好的解决方案:

 // used when editing an item
 public void UploadFiles(FormCollection form, NameValueCollection currentFiles, string folder, bool useTicks)
    {
        foreach (string file in Request.Files)
        {
            var hpf = Request.Files[file];

            if (hpf.ContentLength == 0)
            {
                form[file] = currentFiles[file];
            }
            else
            {
                var filename = useTicks ? hpf.FileName
                    .Replace(" ", "_")
                    .Replace(".", RandomFileName() + ".") : hpf.FileName;

                var myPath = Server.MapPath("~/Content/" + folder);
                hpf.SaveAs(myPath + "/" + filename);

                form[file] = filename;
            }
        }

        if (Request.Files.Count > 0) return;
        foreach (var file in currentFiles.AllKeys)
        {
            form[file] = currentFiles[file];
        }
    }

//used when creating a new item
    public void UploadFiles(FormCollection form, string folder, bool useTicks)
    {

        foreach (string file in Request.Files)
        {
            var hpf = Request.Files[file];

            if (hpf.ContentLength == 0)
            {
                form[file] = null;
            }
            else
            {
                var filename = "";
                filename = useTicks ?
                    hpf.FileName.Replace(" ", "_").Replace(".", RandomFileName() + ".") :
                    hpf.FileName;

                UploadFileName = filename;
                var myPath = Server.MapPath("~/Content/" + folder);
                hpf.SaveAs(myPath + "/" + filename);

                form[file] = UploadFileName;
            }

        }

    }

我使用模型,因此在我的模型项中我使用 UIHint("uploadbox")

这是views/Shared/EditorTemplates/内的代码 这里的UploadField.cshtml

@Html.TextBox("",null,new{type="File"})

是上传功能的使用示例:

public ActionResult AddFiles(FormCollection form, SomeModel myModel)
    {
       UploadFiles(form,"products", true);
        myModel.pdfFile = form["pdffile"];
        myModel.thumbnail = form["thumbnail"];

这是编辑项目时的代码,以防文件没有更改,但其他项目

 var existingFile = ctx2.modelname.SingleOrDefault(x => x.Id == id).Filename;
        NameValueCollection myCol = new NameValueCollection();
        myCol.Add("Filename", existingFile);
        UploadFiles(form, myCol, "uploads/custom", true);

        myModel.Filename = form["Filename"];

只是一个想法:-)

I have never used that method before, and this is no direct answer, but maybe its a better solution:

 // used when editing an item
 public void UploadFiles(FormCollection form, NameValueCollection currentFiles, string folder, bool useTicks)
    {
        foreach (string file in Request.Files)
        {
            var hpf = Request.Files[file];

            if (hpf.ContentLength == 0)
            {
                form[file] = currentFiles[file];
            }
            else
            {
                var filename = useTicks ? hpf.FileName
                    .Replace(" ", "_")
                    .Replace(".", RandomFileName() + ".") : hpf.FileName;

                var myPath = Server.MapPath("~/Content/" + folder);
                hpf.SaveAs(myPath + "/" + filename);

                form[file] = filename;
            }
        }

        if (Request.Files.Count > 0) return;
        foreach (var file in currentFiles.AllKeys)
        {
            form[file] = currentFiles[file];
        }
    }

//used when creating a new item
    public void UploadFiles(FormCollection form, string folder, bool useTicks)
    {

        foreach (string file in Request.Files)
        {
            var hpf = Request.Files[file];

            if (hpf.ContentLength == 0)
            {
                form[file] = null;
            }
            else
            {
                var filename = "";
                filename = useTicks ?
                    hpf.FileName.Replace(" ", "_").Replace(".", RandomFileName() + ".") :
                    hpf.FileName;

                UploadFileName = filename;
                var myPath = Server.MapPath("~/Content/" + folder);
                hpf.SaveAs(myPath + "/" + filename);

                form[file] = UploadFileName;
            }

        }

    }

I use models so in my model item i use the UIHint("uploadbox")

here is the code inside views/Shared/EditorTemplates/UploadField.cshtml

@Html.TextBox("",null,new{type="File"})

here is an example of the usage of the upload feature:

public ActionResult AddFiles(FormCollection form, SomeModel myModel)
    {
       UploadFiles(form,"products", true);
        myModel.pdfFile = form["pdffile"];
        myModel.thumbnail = form["thumbnail"];

here is the code when editing the item, in case the file was not changed, but others items have

 var existingFile = ctx2.modelname.SingleOrDefault(x => x.Id == id).Filename;
        NameValueCollection myCol = new NameValueCollection();
        myCol.Add("Filename", existingFile);
        UploadFiles(form, myCol, "uploads/custom", true);

        myModel.Filename = form["Filename"];

just a thought :-)

~没有更多了~
我们使用 Cookies 和其他技术来定制您的体验包括您的登录状态等。通过阅读我们的 隐私政策 了解更多相关信息。 单击 接受 或继续使用网站,即表示您同意使用 Cookies 和您的相关数据。
原文