C# 不能';t上传文件ASP.NET MVC

C# 不能';t上传文件ASP.NET MVC,c#,asp.net-mvc,file-upload,C#,Asp.net Mvc,File Upload,我正在尝试创建一个创建模型对象页面。我正在使用asp.net mvc和c#。在我的项目中,我试图创建一个包含书籍的图书馆应用程序,我们可以从包含图片的应用程序中添加书籍。我的控制器是这样创建的 [HttpPost] public ActionResult Create([Bind(Exclude = "id,bringBack,borrower,isBorrowed")]Book bookToCreate, HttpPostedFileBase picture1) {

我正在尝试创建一个创建模型对象页面。我正在使用asp.net mvc和c#。在我的项目中,我试图创建一个包含书籍的图书馆应用程序,我们可以从包含图片的应用程序中添加书籍。我的控制器是这样创建的

 [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();
        }
    }
<%@ 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>
我的看法是这样的,

 [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();
        }
    }
<%@ 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>

创造
领域
型号(名称)%%>
型号(名称)%%>
型号(名称)%%>
model.author)%>
model.author)%>
model.author)%>
型号.图片)%>
型号.图片)%>
型号.图片)%>


我尝试了很多东西,但我无法上传任何文件。我能做什么?

尝试从
请求中获取已发布的文件

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

在你的书中,模特应该是一个预科生

HttpPostedFileBase picture1

有什么问题?
picture1
null吗?现在我可以获取文件了,我更改了BeginForm的参数。但我无法保存文件。我能为此做些什么?如何更改文件权限?@Daniel A White,然后提供更好的答案,而不仅仅是批评。@Ömer Faruk AK-您需要更改要保存文件的路径的权限。