使用表单上传文件

1、在Vue中使用multipart/form-data上传文件

	<ul class="upload-imgs">
		<li v-if="imgs.length < 9">
			<input type="file" class="upload" @change="addImg" ref="inputer" multiple accept="image/png,image/jpeg,image/gif,image/jpg" />
			<a class="add">
			   <Icon type="md-add" class="iconfont" />
			</a>
	 	</li>
		<li v-for="(value, key) in imgs" :key="key">
			<p class="img">
			   <img :src="value.url" />
			</p>
		 </li>
	</ul>

  // 获取图片路径
    getImageUrl(file) {
      	var url = null;
      	if (window.createObjcectURL !== undefined) {
        	url = window.createOjcectURL(file);
      	} else if (window.URL !== undefined) {
        	url = window.URL.createObjectURL(file);
      	} else if (window.webkitURL !== undefined) {
        	url = window.webkitURL.createObjectURL(file);
      	}
      	return url;
    },
    
    // 选择图片
    addImg(e) {
      	const file = e.target.files[0];
      	let src = this.getImageUrl(file);  //blob地址
      	let array = this.imgs;
      	let obj = {
        	file: file,
        	name: file.name,
        	url: src
      	}
      	array.push(obj);
      	this.imgs = array;
      	// var reader = new FileReader();
      	// reader.onload = (data) => {
      	// console.log(data.target.result) //base64
      	// };
      	// reader.readAsDataURL(file);
      	e.target.value = ""; // input连续上传同一张
    },
    // 提交
    submitFeebackInfo() {
       var formFile = new FormData();
       formFile.append("content", this.problem); 
       formFile.append("order_no", this.number);
       if (this.imgs.length) {
         this.imgs.forEach(item => {
           formFile.append('image[]', item.file)
         })
       }
       this.$http.submitFeedback(formFile).then((res) => {
         this.$message.success({
           content: res.msg,
         });
       });
    },

2、在uniapp中使用multipart/form-data上传文件

<view class="img_list">
	<image src="../../static/img/repair_add.png" mode="" class="up_icon" @click="chooseImage"></image>
	<view class="img_item" v-for="(item,index) in fileList" :key="index">
		<image :src="item.uri" mode="" class="up_img"></image>
	</view>
</view>

// 选择图片
chooseImage() {
	uni.chooseImage({
		count: 9-this.fileList.length, 
		success: res => {
			let array = this.fileList;
			let arr = res.tempFilePaths.map(item => {
				let obj = {
					uri: item,
					name: 'images[]'
				}
			return obj;
			})
			let files = res.tempFiles;
			for(let i = 0; i < arr.length; i++) {
				arr[i].file = files[i];
			}
			this.fileList = [...arr, ...array];
		}
	});
},

// 提交
submit() {
	let params: {
		title: this.title,
		content: this.content
	}
	let images = [];
	if(this.fileList.length) {
		images = this.fileList;
	}
	uni.uploadFile({
		url: url,
		files: images,
		header: {
			token: token
		},
		formData: params,
		success: res => {
			let response = JSON.parse(res.data);
			if (response.code === 200) {
				console.log(response.data)
			} else {
				uni.showToast({
					title: response.msg,
					position: "center",
					icon: "none",
					duration: 2000
				})
			}
		},
		fail: err => {
			uni.showToast({
				title: "文件上传失败",
				position: "center",
				icon: "none",
				duration: 2000
			})
		}
	})	
}
  • 0
    点赞
  • 0
    收藏
    觉得还不错? 一键收藏
  • 0
    评论

“相关推荐”对你有帮助么?

  • 非常没帮助
  • 没帮助
  • 一般
  • 有帮助
  • 非常有帮助
提交
评论
添加红包

请填写红包祝福语或标题

红包个数最小为10个

红包金额最低5元

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

抵扣说明:

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

余额充值