C#使用Vant UI 的uplaoder组件上传保存图片

页面代码

			 	<van-cell-group>
                    <van-uploader v-model="fileList"
                                  :after-read="afterRead"
                                  multiple
                                  :before-delete="remove"
                                  :max-count="3" />
                </van-cell-group>

方法

			afterRead(file) {
                    let content = file.file;
                    let formData = new FormData();
                    formData.append("upload", content);
                    axios({
                        method: "post",
                        //服务器上传地址
                        url: '/FMFamily/UplaodImg',
                        data: formData,
                        headers: {
                            // 修改请求头
                            "Content-Type": "multipart/form-data"
                        }
                    }).then(res => {
                        if (res.data.Status) {
                            this.imgList.push({ url: res.data.Data, lastModified: content.lastModified });
                        }
                    });
                }
                remove(file) {
                  
                    for (var i = 0; i < this.imgList.length; i++) {
                        if (this.imgList[i].lastModified == file.file.lastModified) {
                            this.imgList.splice(i, 1);
                            this.fileList.splice(i, 1);
                        }
                    }
                },

后台代码

public ActionResult UplaodImg()
        {
            if (Request.Files.Count > 0)
            {
                //名称
                string name = DateTime.Now.ToString("yyyyMMdd");
                //虚拟目录 实际地址
                string pathUrl = Server.MapPath($"~/UploadImg/{name}").ToString();
                // 创建目录c:\name
                if (!Directory.Exists(pathUrl))
                {
                    Directory.CreateDirectory(pathUrl);
                }

                var file = Request.Files["upload"];

                string fileName = file.FileName.Split('.')[0];
                string extension = file.FileName.Split('.')[1];

                string savePath = Server.MapPath($"~/UploadImg/{name}/{fileName}{ConvertHelper.ToString(DateTime.Now, "yyyyMMddHHmmss")}.{extension}");
                string filePath = $"/UploadImg/{name}/{fileName}{ConvertHelper.ToString(DateTime.Now, "yyyyMMddHHmmss")}.{extension}";

                file.SaveAs(savePath);

                return Json(new { Status = true, Data = filePath });
            }

            return Json(new { Status = false });
        }
  • 0
    点赞
  • 1
    收藏
    觉得还不错? 一键收藏
  • 0
    评论
评论
添加红包

请填写红包祝福语或标题

红包个数最小为10个

红包金额最低5元

当前余额3.43前往充值 >
需支付:10.00
成就一亿技术人!
领取后你会自动成为博主和红包主的粉丝 规则
hope_wisdom
发出的红包
实付
使用余额支付
点击重新获取
扫码支付
钱包余额 0

抵扣说明:

1.余额是钱包充值的虚拟货币,按照1:1的比例进行支付金额的抵扣。
2.余额无法直接购买下载,可以购买VIP、付费专栏及课程。

余额充值