1.生成上传控件
<el-upload class="avatar-uploader"
action="/Home/LOADIMAGE?name=aaa123"
:show-file-list="false"
:on-success="handleAvatarSuccess"
:before-upload="beforeAvatarUpload">
<img v-if="imageUrl" :src="imageUrl" class="avatar">
<i v-else class="el-icon-plus avatar-uploader-icon"></i>
</el-upload>
2. 上传图片时改变展示图片
handleAvatarSuccess(res, file) {
this.imageUrl = URL.createObjectURL(file.raw);
},
3. 后端接受后存储到本地
[HttpPost]
public JsonResult LOADIMAGE()
{
var files = Request.Form.Files;
string webRootPath = _hostingEnvironment.WebRootPath;
foreach (var file in files)
{
if (file.Length > 0)
{
var filePath = Path.Combine(webRootPath + "/Images/", file.FileName);
using (var fileStream = new FileStream(filePath, FileMode.Create))
{
file.CopyTo(fileStream);
}
}
}
return Json(0);
}