使用uniapp和uview上传图片,预览和删除都可以

之前写过一个使用uniapp和uview上传图片功能,项目测试时用的那个,是可以上传成功的,但是有点问题,代码优化后把现在的贴出来,感觉比之前那个好用(中配)

优化PLUS版:可以从我的资源处下载依赖免费的,文件中引入,预览方法改为,别的不用改(使用下面的上传组件)

previewImage(logourl) {
        const ctpic = new Ctpic();
        let that = this;
        let res = ctpic.app_URLtoBitmap({
            base64:logourl //base64数据
        })
      }

上传组件,是在插件市场下载的,根据自己需求改了改









父组件,页面中使用,引入后

import gUpload from "../../components/g-upload/g-upload.vue"



				
			

chooseFile(v,which) {
	this.beforeAvatarUpload(v,which)   //v为文件,自动加的,which区分那个上传,页面可能有多个上传
},

beforeAvatarUpload(file,which) {
	let param = {};
	param.imgType = which;
	param.visitId = uni.getStorageSync("visitorItem").id;
	this.$uploadFile({
		url: '', 
		filePath:file,  //上传的文件
		name:"fileName", //后台接受文件标识
		formData:{ //额外参数
			"param":JSON.stringify(param)
		},
		success: (res) => {
			if (res.data.status == 'SUCCESS') {
				this.$refs.uToast.show({
					title: '上传成功',
					type: 'success'
				})
			} else {
				this.$refs.uToast.show({
					title: '上传失败',
					type: 'error'
				})
			}
		},
		fail: (err) => {
			this.$refs.uToast.show({
				title: '网络出了小差,请稍后重试',
				type: 'error '
			})
		}
	});
},

 

你可能感兴趣的:(uniapp,图片上传,删除,预览,上传,javascript)