0

Ich bin Herumspielen mit Datei-Upload asp.net MVC-5 mit HttpPostedFileBase aber es zeigt mir HttpPostedFileBase null ist, nachdem ich das Bild am Auswählen hierasp.net Mvc-5 HttpPostedFileBase ist null

mein Code

<input type="file" title="search image" file-model="profileimage" id="allfilepath" name="file" /> 
<button type="submit" class="btn btn-primary col-sm-5"> 
     <span class="glyphicon glyphicon-plus"></span> 
</button> 

und mein Controller

[HttpPost] 
    public ActionResult insert(HttpPostedFileBase file, quotationcompany quotecomp) 
    { 
     var allowedExtensions = new[] { 
     ".Jpg", ".png", ".jpg", "jpeg" 
    }; 
     if (file == null) 
     { 
      ViewBag.message = "Please Select Image"; 
      return View(); 
     } 
     else { 
      ViewBag.message = "Image Uploaded Successfully"; 
      return View(); 
     } 

    } 

if (Datei == null) (auf dieser Linie (file) sh schuld mich null, nachdem ich png Bild hochgeladen habe)

was ist falsch in diesem Code?

+0

aussehen könnte Ein Rat! Bitte benutze [Search] (https://stackoverflow.com/search?q=HttpPostedFileBase+is+null), bevor du doppelte Fragen stellst, sonst kann deine Frage nach unten gehen (* Da hast du bereits 9 Fragen *) und dies könnte dich veranlassen [Question Ban] (https://stackoverflow.com/help/question-bans) – mmushtaq

Antwort

4

prüfen Formular Attribute

Ein häufiger Fehler, die Menschen machen, ist der folgende Teil in Form Tag fehlt:

<form enctype="multipart/form-data"> 
</form> 

Auch in MVC Ihre Form Struktur so etwas wie dieses

@using (Html.BeginForm("Action", "Controller", FormMethod.Post, new { @enctype = "multipart/form-data", @id = "myForm", @role="form" })) 
+1

'enctype =" multipart/form-data "' ist der richtige Wert, um den Upload von Dateien für HTML zu ermöglichen. –

+0

Richtig du bist. Bearbeitet. Entschuldigung. – Terrance00