1

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

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"); 行返回到 "返回视图(_courseService.ListAllCourses());” 行(实际上并未执行 ListAllCourses)。这意味着它完全取决于内部管道。更糟糕的是,这种延迟与文件大小有关。到底发生了什么,我该如何阻止它?

谢谢

4

1 回答 1

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

只是一个想法 :-)

于 2012-02-06T13:01:45.297 回答