2012-12-06 117 views
5

我想創建一個表單,如果他們希望用戶將編輯有個人檔案並上傳圖片, 這裏是我查看文件上傳在MVC 3總是空

//格式字符串@using(Html.BeginForm("EditProfile","Employee",FormMethod.Post, new { enctype = "multipart/form-data " }))

//輸入HTML <input type="file" name="file" id="file"/>

//提交<input type="submit" name="SubmitBtn" value="SaveProfile" /> <input type="submit" name="SubmitBtn" value="Cancel" />

現在我的控制器

//Profile Modification 
    [Authorize] 
    [HttpPost] 
    public ActionResult EditProfile(employer model, string SubmitBtn,HttpFileCollection file) 
    { 
     switch (SubmitBtn) 
     { 
      case "SaveProfile": 
       try 
       { 
        if (ModelState.IsValid) 
        { 
          //the file is always null 
         if (file != null) 
         { 
          //Function I want to Apply on file 
          model.logoname = logouploded(file); 
          return RedirectToAction("Profile", "Employer"); 
         } 
         return RedirectToAction("EditProfile"); 
        } 
        //ChangeEmployeeProfile(model); 

       } 
       catch (Exception ex) 
       { 
        ViewBag.warn = ex.Message; 
        return View(model); 
       } 
       break; 
      case "Cancel": 
       return RedirectToAction("index"); 
     } 
     return View(model); 
    } 

現在不管我上載的文件的文件總是來空 我也曾嘗試HttpFileCollectionBase在操作功能參數還是文件是空

僅舉員工的模型只包含圖片的文件名 因爲我只想要將圖像名稱保存在數據庫中。

+0

嗨,你有沒有在回發後檢查'Request.Files'集合? –

+2

刪除「multipart/form-data」中的最後一個空格。正確:「multipart/form-data」 –

回答

8

將HttpFileCollection類型更改爲HttpPostedFileBase

+0

修正「multipart/form-data」中的空白後嘗試使用HttpPostedFileBase,但現在文件導致​​modelstate.isvalide()返回false。 –

+0

Message =「從類型'System.Web.HttpPostedFileWrapper'到類型'System.Web.HttpPostedFile'的參數轉換失敗,因爲沒有類型轉換器可以在這些類型之間進行轉換。」 –

+1

終於它的工作感謝您的幫助 在「multipart/form-data」中的空白是問題,參數被改爲HttpPostedFileBase,它的工作像魅力再次感謝@peter Kiss –