ASP.NET MVC提供程序托管应用程序-上传文件

3

我有一个提供方托管的应用程序(asp.net MVC),正在尝试使用表单上传文件,但是什么也不起作用,即HttpPostedFileBase始终为NULL。 请帮忙。

这是我正在使用的代码,

@using (Html.BeginForm("Create", "StudentProgress", routeValues,FormMethod.Post, new { enctype = "multipart/form-data" }))
{
    <div class="modal-body">
        @Html.AntiForgeryToken()

        <div class="form-horizontal">

            @Html.ValidationSummary(true)
            @Html.HiddenFor(model => model.fk_level_register)
            @Html.HiddenFor(model => model.date_modified)
            @Html.HiddenFor(model => model.done_by)


            <label for="file">Filename:</label>
            <input type="file" name="upload" id="file" style="width:240px" />

            <div class="form-group">
                @Html.LabelFor(model => model.submission_deadline_date, htmlAttributes: new { @class = "control-label col-md-2" })
                <div class="col-md-10">
                    @Html.EditorFor(model => model.submission_deadline_date)
                    @Html.ValidationMessageFor(model => model.submission_deadline_date)
                </div>
            </div>

            <div class="form-group">
                @Html.LabelFor(model => model.period_covered, htmlAttributes: new { @class = "control-label col-md-2" })
                <div class="col-md-10">
                    @Html.TextBoxFor(model => model.period_covered, new { @class = "form-control" })
                    @Html.ValidationMessageFor(model => model.period_covered)
                </div>
            </div>
}
2个回答

0

可以使用HttpPostFileBase数据类型,而不是将文件属性作为字符串数据类型使用。

ClassModel:

public DateTime submission_deadline_date { get; set; }
public string period_covered { get; set; }
public HttpPostedFileBase File { get; set; }
public string fk_level_register { get; set; }
public DateTime date_modified { get; set; }
public string done_by { get; set; }

控制器:

[HttpPost]
    public ActionResult Create(Period collection)
    {
        string fpath = "";
        try
        {
            if (Request.Files.Count > 0)
            {
                try
                {

                    HttpPostedFileBase file = collection.File;
                    string fname;
                    // want to check extension then use this method
                    //if (CheckExtension(file.ContentType.ToLower()))
                    //{
                        // Checking for Internet Explorer  
                        string extension = System.IO.Path.GetExtension(file.FileName);

                        if (Request.Browser.Browser.ToUpper() == "IE" || Request.Browser.Browser.ToUpper() == "INTERNETEXPLORER")
                        {
                            string[] testfiles = file.FileName.Split(new char[] { '\\' });
                            fname = Guid.NewGuid() + extension; //+ testfiles[testfiles.Length - 1];
                        }
                        else
                        {
                            fname = Guid.NewGuid() + extension; //+ file.FileName;
                        }

                        // Get the complete folder path and store the file inside it.  
                        fname = System.IO.Path.Combine(Server.MapPath("~/Content/"), fname);
                        file.SaveAs(fname);

                        return Content("Successfully Uploaded");
                    //}

                }
                catch (Exception ex)
                {
                    return Content("Error occurred. Error details: " + ex.Message);
                }
            }

        }
        catch (Exception)
        {
            return Content(Response.StatusCode.ToString() + " Bad Requrest error." + fpath);
        }
        return Content("No files selected.");
    }

    public bool CheckExtension(string Ext)
    {
        if (Ext == "application/pdf")
        {
            return true;
        }
        else if (Ext == "text/plain")
        {
            return true;
        }
        else if (Ext == "application/msword")
        {
            return true;
        }
        else if (Ext == "application/vnd.openxmlformats-officedocument.wordprocessingml.document")
        {
            return true;
        }
        else
        {
            return false;
        }
    }

视图:在视图中,您只需要做出小的更改即可。

@using (Html.BeginForm("Create", "Home", null, FormMethod.Post, new { id = "formU", enctype = "multipart/form-data" })){

 @Html.AntiForgeryToken()
<div class="modal-body">
    <div class="form-horizontal">
        @Html.ValidationSummary(true)
        @Html.HiddenFor(model => model.fk_level_register)
        @Html.HiddenFor(model => model.date_modified)
        @Html.HiddenFor(model => model.done_by)


        <label for="file">Filename:</label>
        <input type="file" name="File" id="File" style="width:240px" />
        @*@Html.EditorFor(model => model.File, new { htmlAttributes = new { @type="file", @class = "form-control", @placeholder = "File path" } })*@

        <div class="form-group">
            @Html.LabelFor(model => model.submission_deadline_date, htmlAttributes: new { @class = "control-label col-md-2" })
            <div class="col-md-10">
                @Html.EditorFor(model => model.submission_deadline_date)
                @Html.ValidationMessageFor(model => model.submission_deadline_date)
            </div>
        </div>

        <div class="form-group">
            @Html.LabelFor(model => model.period_covered, htmlAttributes: new { @class = "control-label col-md-2" })
            <div class="col-md-10">
                @Html.TextBoxFor(model => model.period_covered, new { @class = "form-control" })
                @Html.ValidationMessageFor(model => model.period_covered)
            </div>
        </div>
        <div class="form-group">
            <input id="submit" type="submit" value="Submit" class="btn btn-success" />
        </div>
    </div>
</div>}

您也可以从 GitHub 存储库 这里 下载源代码。


0

网页内容由stack overflow 提供, 点击上面的
可以查看英文原文,
原文链接