当我上传图像时,它从视图中向我发送了错误,我不知道如何解决?



当我运行它时,它不是调用上下文类,也不是去控制器,它给了我以下错误,我不知道如何删除它。

输入不是有效的 Base-64 字符串,因为它包含非 base 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();
}

请告诉我这个错误以及我如何解决它,并告诉我什么是内部异常以及如何处理它们以及为什么以及何时调用它们。

在模型中,将byte更改为 fileAddress 的string,并在应用程序的根目录中创建上传文件夹:

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();
}

相关内容

最新更新