MVC4检索上传的文件

本文关键字:文件 检索 MVC4 | 更新日期: 2023-09-27 18:04:45

我正在使用ASP。NET MVC4,以便开发一个intranet应用程序。其中一个主要功能是允许用户上传文件,这些文件将存储在我的数据库中。为了做到这一点,我使用jQuery。然而,我不知道我必须做些什么来操纵上传的文件。我已经知道我必须将它们操纵到相应的控制器中,但是在互联网上阅读了一些提示之后,我只是看不出我应该怎么做。

这是我的观点:

    @model BuSIMaterial.Models.ProductAllocationLog
@{
    ViewBag.Title = "Create";
}
<h2>Create</h2>
@using (Html.BeginForm())
{
    @Html.ValidationSummary(true)
    <fieldset>
        <legend>ProductAllocationLog</legend>
        <div class="editor-label">
            Date :
        </div>
        <div class="editor-field">
            @Html.TextBoxFor(model => model.Date, new { @class = "datepicker"})
            @Html.ValidationMessageFor(model => model.Date)
        </div>
        <div class="editor-label">
            Message :
        </div>
        <div class="editor-field">
            @Html.TextAreaFor(model => model.Message)
            @Html.ValidationMessageFor(model => model.Message)
        </div>
        <div class="editor-label">
            Allocation :
        </div>
        <div class="editor-field">
            @Html.DropDownList("Id_ProductAllocation", String.Empty)
            @Html.ValidationMessageFor(model => model.Id_ProductAllocation)
        </div>
        <div class="demo" style="float:left; margin-top:5px;">
            <div id="aupload" style = "border:2px dashed #ddd; width:100px; height:100px; margin-right:10px; padding:10px; float:left;"></div>
            <div id="uploaded" style = "border: 1px solid #ddd; width:550px; height:102px; padding:10px; float:left; overflow-y:auto;"></div>
        </div>
        <p>
            <input type="submit" value="Create" />
        </p>
    </fieldset>
}
<div>
    @Html.ActionLink("Back to List", "Index")
</div>

我绝对不是要求预先制作的代码样本,但只是为了一种方式来进行。那真是太好了。

MVC4检索上传的文件

您需要做三件事。首先,在视图中添加一个图像上传字段:

<input type="file" name="file-upload" />

. .把你的表单改成multipart .

@using (Html.BeginForm("Action", "Controller", null, FormMethod.Post, new { enctype = "multipart/form-data" }))
{
}

…然后在控制器中,通过请求对象上的'Files'集合访问该文件:

Request.Files["file-upload"];

如果您希望使用ajax/jquery提交表单,那么要序列化这些文件还需要做一些工作。使用jQuery.ajax

发送multipart/formdata
  @model MVCDemo.Models.tbl_Images
@{
    ViewBag.Title = "Create";
}
<h2>Create</h2>
@using (Html.BeginForm())
{
    @Html.ValidationSummary(true)
    <fieldset>
        <legend>ProductAllocationLog</legend>
        <div class="editor-label">
            Date :
        </div>
        <div class="editor-field">
            @Html.TextBoxFor(model => model.Date, new { @class = "datepicker"})
            @Html.ValidationMessageFor(model => model.Date)
        </div>
        <div class="editor-label">
            Message :
        </div>
        <div class="editor-field">
            @Html.TextAreaFor(model => model.Message)
            @Html.ValidationMessageFor(model => model.Message)
        </div>
        <div class="editor-label">
            Allocation :
        </div>
        <div class="editor-field">
            @Html.DropDownList("Id_ProductAllocation", String.Empty)
            @Html.ValidationMessageFor(model => model.Id_ProductAllocation)
        </div>
        <div class="demo" style="float:left; margin-top:5px;">
            <div id="aupload" style = "border:2px dashed #ddd; width:100px; height:100px; margin-right:10px; padding:10px; float:left;"></div>
            <div id="uploaded" style = "border: 1px solid #ddd; width:550px; height:102px; padding:10px; float:left; overflow-y:auto;"></div>
        </div>
        <p>
            <input type="submit" value="Create" />
        </p>
    </fieldset>
}
<div>
    @Html.ActionLink("Back to List", "Index")
</div>