主要是為了上傳文件路徑,而HTML5在其他好多的瀏覽器中不能直接返回路徑給你。
主要思路:把文件上傳到服務(wù)器的指定文件夾中,然后拼接字符串拼接出它在服務(wù)器的路徑,然后保存到數(shù)據(jù)庫。
Html代碼:
}
控制器代碼:
public ActionResult UploadFile()
{
string path =Server.MapPath(~/Content/img/);//設(shè)定上傳的文件路徑
if (!Directory.Exists(path))
{
Directory.CreateDirectory(path);
}
//判斷是否已經(jīng)選擇上傳文件
HttpPostedFileBase file =Request.Files[file];
string filenName =file.FileName;
string filepath = path +filenName;
file.SaveAs(filepath);//上傳路徑
//然后你就可以用filepath新增到數(shù)據(jù)庫里面了。
return Content();
}