uniapp 微信小程序选择上传非图片文件微信文件

uniapp yekong 98℃
<template>
	<div @click="chooseFile">
		<slot></slot>
	</div>
</template>

<script>
	import configs from '@/config/config.js'
	export default {
		data() {
			return {}
		},
		props: {},
		methods: {
			chooseFile() {
				var that = this;
				wx.chooseMessageFile({
					count: 1, //默认100
					success: function(res) {
						console.log(JSON.stringify(res));
						console.log(res.tempFiles[0]);
						that.uploadimg(res.tempFiles[0].path)
					}
				});
			},

			uploadimg(file) {
				var that = this;
				uni.showLoading({
					title: '上传中'
				});
				uni.uploadFile({
					url: configs.uploadFile,
					filePath: file,
					name: 'file',
					header: {
						cookie: uni.getStorageSync('cookie')
					},
					success(res) {
						uni.hideLoading();
						console.log(res);
						let datas = JSON.parse(res.data);
						if (datas.code == 1) {
							uni.showToast({
								title: '上传成功',
								duration: 2000
							});
							that.$emit('upload', datas.data.fileUrl);
						} else {
							uni.showToast({
								title: datas.msg,
								icon: 'none',
								duration: 2000
							});
						}
					},
					fail() {
						uni.hideLoading();
					}
				});
			}
		}
	}
</script>

<style>
</style>

喜欢 (0)