2014-03-24 44 views
1

我查看了一些其他的答案,並找不到解決方案。我似乎無法弄清楚爲什麼。這是使用Bootstrap 3.0。我想要做的就是使用它來上傳新的頭像圖片。問題是它總是出現空,我似乎無法弄清楚爲什麼。使用最近的Bootstrap文件上傳和mvc asp.net上傳文件

這裏是我的HTML:

@using (Html.BeginForm("EditAvatar", "Profile", new { userID = @Model.ProPit_User.userID }, FormMethod.Post, new { })) 
{ 
    <div class="form-group"> 
     <div class="fileinput fileinput-new" data-provides="fileinput"> 
      <div class="fileinput-new thumbnail" style="width: 200px; height: 150px;"> 
       <img src="http://www.placehold.it/200x150/EFEFEF/AAAAAA&amp;text=no+image" alt="" /> 
      </div> 
      <div class="fileinput-preview fileinput-exists thumbnail" style="max-width: 200px; max-height: 150px;"> 
      </div> 
      <div> 
       <span class="btn default btn-file"> 
        <span class="fileinput-new">Select image 
        </span> 
        <span class="fileinput-exists">Change 
        </span> 
        <input id="avatar_image" type="file" name="..." runat="server"> 
       </span> 
       <a href="#" class="btn default fileinput-exists" data-dismiss="fileinput">Remove 
       </a> 
      </div> 
     </div> 
     <div class="clearfix margin-top-10"> 
      <span class="label label-danger">NOTE! 
      </span> 
      <span>Attached image thumbnail is supported in Latest Firefox, Chrome, Opera, Safari and Internet Explorer 10 only 
      </span> 
     </div> 
    </div> 
    <div class="margin-top-10"> 
     <button type="submit" class="btn green"> 
      Save Changes 
     </button> 
     <button type="reset" class="btn default">Cancel</button> 
    </div> 
} 

我已經給文件輸入的avatar_image

這裏將ID控制器:

[HttpPost] 
     public ActionResult EditAvatar(HtmlInputFile avatar_image) 
     { 

      if (avatar_image.PostedFile != null) 
      { 
       //do whatever you want with the file 
      } 

      return View(); 
     } 

當尋找斷點處的avatar_image .PostedFile始終爲空。任何人有任何想法我失蹤?

+0

請求在Fiddler中看起來像什麼?另外,將enctype =「multipart/form-data」屬性添加到表單中。 –

+0

@SteveAndrews不知道從來沒有使用過提琴手,我必須下載它,看看它是否能提供任何東西。我將添加Enctype並查看是否有幫助。 –

+0

@SteveAndrews這是它的主體。這使它工作。謝謝。 –

回答

3

您可以在Fiddler中檢查請求。更重要的是,您需要將enctype =「multipart/form-data」屬性添加到表單中。