繁体   English   中英

MVC4检索上传的文件

[英]MVC4 retrieve uploaded files

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

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

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

<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提交表单,则需要做更多的工作来序列化文件。 这篇文章将为您提供帮助: 使用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>

暂无
暂无

声明:本站的技术帖子网页,遵循CC BY-SA 4.0协议,如果您需要转载,请注明本站网址或者原文地址。任何问题请咨询:yoyou2525@163.com.

 
粤ICP备18138465号  © 2020-2024 STACKOOM.COM