4

我正在一个列表站点上工作,我正在尝试上传图像并将其路径存储在数据库中。

问题是,我似乎无法从表单中检索上传的文件。

这是表格,

@using (Html.BeginForm())
{
    @Html.AntiForgeryToken()
    @Html.ValidationSummary(true)

    <fieldset>
        <legend>Product</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">
            @Html.LabelFor(model => model.Description)
        </div>
        <div class="editor-field">
            @Html.EditorFor(model => model.Description)
            @Html.ValidationMessageFor(model => model.Description)
        </div>

        <div class="editor-label">
            @Html.LabelFor(model => model.Location)
        </div>
        <div class="editor-field">
            @Html.EditorFor(model => model.Location)
            @Html.ValidationMessageFor(model => model.Location)
        </div>

        <div class="editor-label">
            @Html.LabelFor(model => model.Condition)
        </div>
        <div class="editor-field">
            @Html.EditorFor(model => model.Condition)
            @Html.ValidationMessageFor(model => model.Condition)
        </div>

        <div class="editor-label">
            @Html.LabelFor(model => model.Price)
        </div>
        <div class="editor-field">
            @Html.EditorFor(model => model.Price)
            @Html.ValidationMessageFor(model => model.Price)
        </div>

        <div class="editor-label">
            @Html.LabelFor(model => model.PostedBy)
        </div>
        <div class="editor-field">
            @Html.EditorFor(model => model.PostedBy)
            @Html.ValidationMessageFor(model => model.PostedBy)
        </div>

        <div class="editor-field">
            Upload Image <input type="file" name="listingImage" />
        </div>

        <p>
            <input type="submit" value="Create" />
        </p>
    </fieldset>
}

这是行动

[HttpPost]
    [ValidateAntiForgeryToken]
    public ActionResult Create(Product product, HttpPostedFileBase listingImage)
    {
        if (listingImage == null)
        {
            return RedirectToAction("Index");
        }
        if (ModelState.IsValid)
        {
            db.Products.Add(product);
            db.SaveChanges();
            return RedirectToAction("Index");
        }

        return View(product);
    }

我有一个这样的产品列表设置模型。

 public class Product
{
    public int ID { get; set; }

    public string Name { get; set; }
    public string Description { get; set; }
    public string Location { get; set; }
    public string Condition { get; set; }
    public decimal Price { get; set; }
    public string PostedBy { get; set; }

    public string PhotoPath { get; set; }
}

现在每次我完成表单并提交listingImage 时,我都会返回到索引。

有什么我想念的吗?

谢谢。

4

2 回答 2

2

您必须将表单编码类型设置为“multipart/form-data”,否则无法发布文件。

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

是的,您可以将文件与模型数据分开定义并像以前一样声明控制器操作,无需在模型类中包含文件字段:

public ActionResult Create(Product product, HttpPostedFileBase listingImage)
于 2013-07-06T17:39:27.680 回答
1

listingImage不是模型的一部分,因此它不会获得与其余Model.xx属性相同的绑定。

您可以更新Product以包含HttpPostedFileBase listingImage控制器操作并将其更改为

public ActionResult Create(Product product)

在您的 html 中,您可以像这样添加新属性

@using (Html.BeginForm("Create", "Products", FormMethod.Post, new { enctype = "multipart/form-data" })){ @* Forgot this line as Alex Skalozub told in his answer  *@ 
@* code omitted *@

    <div class="editor-field">
        @Html.TextBoxFor(model => model.listingImage, new {type = "file"})
    </div>
}

对于文件位置和保存,您可以执行以下操作

var path = Path.Combine(Server.MapPath("~/images/products"),product.listingImage.FileName);
product.listingImage.SaveAs(path);
于 2013-07-06T16:48:22.643 回答