2017-04-21 36 views
0

當我運行這個它不是調用上下文類,而不是去控制器,它給了我下面給出的錯誤,我不知道如何刪除它。當我上傳圖片時,它發送給我錯誤的意見,我不知道如何解決它?

輸入不是有效的Base-64字符串,因爲它包含非基本64字符,多於兩個填充字符或填充字符中的非法字符。

同時告訴我關於內部例外的原因以及我們如何解決它們。

public class Employee 
      { 
       [Key] 
       public int EmpID { get; set; } 
       [Required] 
       [Display(Name = "First Name")] 
       public string First_Name { get; set; } 
       [Required] 
       [Display(Name = "Last Name")] 
       public string Last_Name { get; set; } 
       [Display(Name ="Image")] 
       public byte[] Images { get; set; } 
     } 

     public class CareerContext:DbContext 
      { 

       public DbSet<Employee> Employees { get; set; } 
      } 

     public ActionResult Registration(Employee Emp) 
       { 
        HttpPostedFileBase image = Request.Files["Image"]; 
        if (ModelState.IsValid) 
        { 
         if (image != null) 
         { 
          Emp.Images = new byte[image.ContentLength]; 
          image.InputStream.Read(Emp.Images, 0, image.ContentLength); 
         } 
         db.Employees.Add(Emp); 
         db.SaveChanges(); 
         return RedirectToAction("Resume"); 
        } 
        return View(); 
       } 

請告訴我這個錯誤,我怎麼能解決這個問題,並告訴我什麼是內部異常以及如何處理他們,爲什麼,當他們叫。

+0

您的問題unclaer,普萊舍後有什麼異常即將到來,在那裏它是未來 –

+0

朋友告訴我什麼是內部異常,以及我們如何處理它們@ Sujit.Warrier –

+0

@ZeeshanHaider告訴我你的文件上傳您的文件CSHTML – Valkyrie

回答

0

在你的模型string改變byte爲fileAddress和您的應用程序的根目錄下創建上傳文件夾:

public ActionResult Registration(Employee Emp) 
    { 
     HttpPostedFileBase image = Request.Files["Image"]; 
     if (ModelState.IsValid) 
     { 
      if (image != null && image.ContentLength > 0) 
      { 
       string path = Path.Combine(Server.MapPath("~/Upload"), 
           Path.GetFileName(image.FileName + Guid.NewGuid().ToString("N"))); 
       image.SaveAs(path); 
       //now save your entity in database `path` is the address of your file 
       //var empolyee = new Employee { Images = path }; 
      } 
     } 
     return View(); 
    } 
+0

通過使用這段代碼我們如何從數據庫中檢索圖片和文件並在視圖中顯示它。 @Valkyriee –

+0

什麼是「N」@Valkyriee –

+0

現在這個執行是拋出。 mscorlib.dll中發生類型'System.IO.DirectoryNotFoundException'的異常,但未在用戶代碼中處理 附加信息:無法找到部分路徑'E:\ MVC概念和項目\ CareersFYP \ CareersFYP \ Upload \ webdevelopercover letter.docx69f9f3b3-1afa-4ef4-8219-cc3478adb5d9'。 –

相關問題