数据库储存照片(文件)和mvc中的导入
一:数据库中储存照片的两种方法:
(1):将照片的名字储存在数据库中,照片储存在项目中;
(2):将照片转换成二进制数据储存在数据库中;
// HttpPostedFileBase 用于接收客户端(浏览器)传递过来的文件的
public ActionResult InsertStudent(HttpPostedFileBase upImage)
{
(1): 将照片的名字储存在数据库中,照片储存在项目中;
//检查文件存放目录是否存在
// Directory.Exists(物理路径)
// Server.MapPath("虚拟路径")==转化为==>物理路径 {就是将相对路径转化为绝对路径}
if (!System.IO.Directory.Exists(Server.MapPath("~/Document/studentPicture/")))
{
System.IO.Directory.CreateDirectory(Server.MapPath("~/Document/studentPicture/"));
}
//判断是否上传了文件
// upImage.ContentLength判断文件的大小
if (upImage != null && upImage.ContentLength > 0)
{
//创建扩展名
string srtrEx = System.IO.Path.GetExtension(upImage.FileName);
//创建文件名称
string fileName = DateTime.Now.ToString("yyyyMMddHHmmssffff") + "_" + Guid.NewGuid().ToString() + srtrEx;
//创建文件的路径
string filePath = Server.MapPath("~/Document/studentPicture/") + fileName;
//将照片名称保存到项目中
upImage.SaveAs(fileP