视图: <div class="form-group">
控制器: var file= Request.Files["tupian1"];——定义一个变量接收图片
@Html.LabelFor(model => model.tupian, htmlAttributes: new { @class = "control-label col-md-2" })
<div class="col-md-10">
<input type="file" name="tupian1" />
@Html.ValidationMessageFor(model => model.tupian, "", new { @class = "text-danger" })
</div>
</div>
控制器: var file= Request.Files["tupian1"];——定义一个变量接收图片
if (file.ContentLength > 0)——如果有数据成功上传
{
string fileName = Guid.NewGuid().ToString("N") + System.IO.Path.GetExtension(file.FileName);——随机生成新文件名+原文件名的后缀
string path="/images/" + fileName;——存放路径+文件名
student.tupian = path;
path = Server.MapPath(path);——获得该地址的绝对路径
file.SaveAs(path);——保存
上传图片
原文地址:https://www.cnblogs.com/liangliping/p/9065793.html