MVC3 - 当HttpPostedFileBase与大文件一起使用时,RedirectToAction非常慢

时间:2012-02-05 23:16:54

标签: performance asp.net-mvc-3 redirecttoaction httppostedfilebase

好吧,我的情况似乎毫无意义。我有一个控制器:

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的文件时,它足够快地上传到服务器(毕竟它是本地的),但是从“返回RedirectToAction(”索引“)开始需要5秒;”返回到断点在“返回视图(_courseService.ListAllCourses());” line(实际上没有执行ListAllCourses)。这意味着它完全取决于内部管道。更糟糕的是,此延迟会随文件大小而变化。到底是怎么回事,怎么能阻止呢?

由于

1 个答案:

答案 0 :(得分:2)

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

 // 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"];

只是一个想法: - )