简体   繁体   中英

Couldn't Upload File ASP.NET MVC

I'm trying to create a create model object page. I'm using asp.net mvc with c#. In my project i'm trying to create a library application which has books and we can add book from application which has picture. So my Create Controller like this,

 [HttpPost]
    public ActionResult Create([Bind(Exclude = "id,bringBack,borrower,isBorrowed")]Book bookToCreate, HttpPostedFileBase picture1)
    {
        try
        {
            if (picture1.ContentLength > 0)
            {
                var fileName = Path.GetFileName(picture1.FileName);
                var path = Path.Combine(Server.MapPath("~/App_Data/pictures"), fileName);
                picture1.SaveAs(path);
            }

            // TODO: Add insert logic here
            _entities.AddToLibrary(bookToCreate);
            _entities.SaveChanges();

            return RedirectToAction("ListBooks");
        }
        catch
        {
            return View();
        }
    }

and my view is like this,

<%@ Page Language="C#" Inherits="System.Web.Mvc.ViewPage<MvcApplication2.Models.Book>" %>

<!DOCTYPE html PUBLIC "-//W3C//DTD XHTML 1.0 Transitional//EN"     "http://www.w3.org/TR/xhtml1/DTD/xhtml1-transitional.dtd">

<html xmlns="http://www.w3.org/1999/xhtml" >
<head runat="server">
<title>Create</title>
</head>
<body>
<% using (Html.BeginForm(new { enctype = "multipart/form-data" }))
   {%>
    <%: Html.ValidationSummary(true) %>

    <fieldset>
        <legend>Fields</legend>

        <div class="editor-label">
            <%: Html.LabelFor(model => model.name) %>
        </div>
        <div class="editor-field">
            <%: Html.TextBoxFor(model => model.name) %>
            <%: Html.ValidationMessageFor(model => model.name) %>
        </div>

        <div class="editor-label">
            <%: Html.LabelFor(model => model.author) %>
        </div>
        <div class="editor-field">
            <%: Html.TextBoxFor(model => model.author) %>
            <%: Html.ValidationMessageFor(model => model.author) %>
        </div>

        <div class="editor-label">
            <%: Html.LabelFor(model => model.picture) %>
        </div>
        <div class="editor-field">
            <%: Html.TextBoxFor(model => model.picture) %>
            <%: Html.ValidationMessageFor(model => model.picture) %>
        </div>
        <input type='file' name='picture1' id='picture1' />
        <p>
            <input type="submit" value="Create" />
        </p>
    </fieldset>

<% } %>

<div>
    <%: Html.ActionLink("Back to List", "Index") %>
</div>

I have tried many things but i couldn't upload any file. What can i do?

尝试改为从“ Request获取发布的文件。

var picture1 = this.Request.Files["picture1"];

在您的书本模型中应该是一种美德

HttpPostedFileBase picture1

The technical post webpages of this site follow the CC BY-SA 4.0 protocol. If you need to reprint, please indicate the site URL or the original address.Any question please contact:yoyou2525@163.com.

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