2012-06-04 7 views
0

모델 만들기 페이지를 만들려고합니다. 나는 asp.net mvc C# 사용하고 있습니다. 내 프로젝트에서는 책이있는 라이브러리 응용 프로그램을 만들려고하고 있으며 그림이있는 응용 프로그램에서 책을 추가 할 수 있습니다. 그래서,파일을 업로드 할 수 없습니다. ASP.NET MVC

[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> 

나는 많은 것들을 시도했지만 내가 모든 파일을 업로드 할 수

. 내가 무엇을 할 수 있을지?

+1

문제가 무엇을해야 하는가? 'picture1'은 널입니까? –

답변

0

게시 된 파일을 Request에서 가져 오는 것이 좋습니다. 책 모델에서

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

-1 매우 mvc 친화적이지 않습니다. –

+0

이제 파일을 가져올 수 있습니다. BeginForm의 매개 변수가 바뀝니다. 하지만 파일을 저장할 수 없습니다. 이걸 위해 내가 뭘 할 수 있을까? 파일 권한을 어떻게 변경할 수 있습니까? –

+0

@Daniel A White 그렇다면 비난하기보다는 더 나은 대답을하십시오. – Gabe

0

는 preperty

HttpPostedFileBase picture1 
관련 문제