2009-04-19 7 views
9

Mit dem folgenden Markup aus meiner Sicht:Datei laden MVC

<form action="Categories/Upload" enctype="multipart/form-data" method="post"> 
    <input type="file" name="Image"> 
    <input type="submit" value"Save"> 
</form> 

Und in meinem Controller:

public ActionResult Upload(FormCollection form) 
{ 
    var file = form["Image"]; 
} 

Der Wert der Datei ist null. Wenn ich es in einer anderen Ansicht versuche mit einem anderen Controller Controller und es funktioniert mit dem gleichen Code.

Ich habe VS2008 auf Vista, MVC 1.0.

Warum?

Malcolm

+7

„niemand wird habe eine Antwort "- ??? –

+0

Gut die 2 gegebenen Antworten tun nicht und ich setze Geld auf das niemand es SO löst. – Malcolm

+0

Antwort ist eine, die ein Problem richtig löst – Malcolm

Antwort

6

Versuchen Sie diesen Code:

public ActionResult Upload() 
    { 
     foreach (string file in Request.Files) 
     { 
      var hpf = this.Request.Files[file]; 
      if (hpf.ContentLength == 0) 
      { 
       continue; 
      } 

      string savedFileName = Path.Combine(
       AppDomain.CurrentDomain.BaseDirectory, "PutYourUploadDirectoryHere"); 
       savedFileName = Path.Combine(savedFileName, Path.GetFileName(hpf.FileName)); 

      hpf.SaveAs(savedFileName); 
     } 

    ... 
    } 
+0

Nein Request.Files ist leer ???? – Malcolm

+0

Sie benötigen keine Request.Files. Siehe diese Antwort: http://stackoverflow.com/questions/765211/file-upload-mvc/765308#765308 – bzlm

+0

Sie benötigen Request.Files, wenn Sie mehrere Dateien hochladen wollen. –

34

Verwenden HttpPostedFileBase als Parameter auf Ihrer Aktion. Fügen Sie außerdem das AcceptVerb-Attribut hinzu, das auf POST festgelegt ist.

[AcceptVerbs(HttpVerbs.Post)] 
public ActionResult Upload(HttpPostedFileBase image) 
{ 
    if (image != null) { 
     // do something 
    } 
    return View(); 
} 

Dieser Code ist ziemlich im Geist/Design von ASP.NET MVC.

+2

Ich habe gerade ein paar Stunden im Kreis verbracht, weil mein Dateieingabetag ein "ID =" - Attribut, aber kein "NAME =" hatte - stelle sicher, dass du "name = ..." eingibst oder die Datei zum actionresult posten wird , wird aber null sein. Hoffe, das hilft jemandem. – Losbear

7

nicht pingelig hier oder etwas zu sein, aber hier ist, wie der Code sollte, aussehen, als Daniel ein paar kleineren Details im Code fehlte er geliefert hat ...

[AcceptVerbs(HttpVerbs.Post)] 
public ActionResult UploadPlotImage(HttpPostedFileBase image) 
{  
    if (image != null) 
    {   
     // do something  
    } 

    return View(); 
} 
+0

Was fehlt Daniel? – KTF

+1

Meine Vermutung ist, dass Daniel etwas vermisst hat, aber Bretts Post gesehen und seine Antwort modifiziert hat. –

+1

Grammatik Polizei! "UploadPlotImadge" >>> "UploadPlotImage" lol =) – Losbear

2

Auch war ich vor einem Problem , Der Wert wurde auf

in Bild null
public ActionResult UploadPlotImadge(HttpPostedFileBase image) 

Früher ich nicht [AcceptVerbs(HttpVerbs.Post)] hinzugefügt haben, die ich hinzu. Auch nach dem Hinzufügen, es hat nicht funktioniert, weil der zweite Teil I fehlte, enctype="multipart/form-data", in dem Formular-Tag sein musste ..

Jetzt funktioniert es für mich ....

+0

enctype = "multipart/form-data" war auch für mich entscheidend, warum braucht das nicht jeder? – Slider345

0

diese Klasse versuchen, unterhalb der Aktion und fixiere den Ordnerpfad in AppSetting.

config:

<appSettings> 
      <add key="UploadFolerPath" value="..Your folder path" /> 
    </appSettings> 

Blick: -

<form action="Account/AddImage" id="form_AddImage" method="post" enctype="multipart/form-data"> 

      <input type="file" id="Img" name="Img" class="required" /> 

      <input type="submit" value="Upload" id="btnSubmit" /> 

</form> 

Klasse: -

public class FileUpload 
{ 
    public string SaveFileName 
    { 
     get; 
     set; 
    } 


    public bool SaveFile(HttpPostedFileBase file, string FullPath) 
    { 
     string FileName = Guid.NewGuid().ToString(); 

     FileName = FileName + System.IO.Path.GetExtension(file.FileName); 

     SaveFileName = FileName; 

     file.SaveAs(FullPath + "/" + FileName); 
     return true; 
    } 
} 

// Beitrag Aktion

[HttpPost] 
    public ActionResult AddImage(FormCollection Form) 
    { 

     FileUpload fileupload = new FileUpload(); 
     var image=""; 

     HttpPostedFileBase file = Request.Files["Img"]; 

     if (file.FileName != null && file.FileName != "") 
     { 

      if (upload.ContentLength > 0) 
      { 

        fileupload.SaveFile(Request.Files["Img"], Server.MapPath(AppSetting.ReadAppSetting("UploadFolerPath"))); 

       image = fileupload.SaveFileName; 

       // write here your Add/Save function 

       return Content(image); 


      } 
     } 
     else 
     { 
        //return....; 
     } 

    }