0

我正在使用 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>

我绝对不是要求预先制作的代码示例,而只是要求一种继续进行的方法。这将是非常友好的。

4

2 回答 2

0

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

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

..使您的表格“多部分”..

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

...然后在您的控制器中,通过请求对象上的“文件”集合访问文件:

Request.Files["file-upload"];

如果您希望使用 ajax/jquery 提交表单,则需要做更多工作来序列化文件。这篇文章将帮助您:Sending multipart/formdata with jQuery.ajax

于 2013-03-20T11:06:18.210 回答
0
  @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>
于 2014-02-19T14:51:00.280 回答