【uniapp】「微信小程序」导入&导出

导出(下载)

在这里插入图片描述
在这里插入图片描述

// 下载文件资源
var timestamp = new Date().getTime();
uni.downloadFile({
	url: 'https://example.com/audio/123', //仅为示例,并非真实的资源
    header:{ token:uni.getStorageSync('token'),"Content-Type":'application/vnd.ms-excel'},
    filePath: 'wxfile://temp/' + timestamp + '.xls',
	success (res) {
		// 保存文件资源
		wx.saveFileToDisk({
			filePath: res.tempFilePath,
            filePath: 'wxfile://temp/' + timestamp + '.xls',
			success:(ret)=>{
				console.log(ret)
				that.toast('保存成功');
			},
			fail:(rey)=>{
				console.log(rey)
				that.toast('保存失败');
			}
		})
	}
})

导入(上传、选择文件)

在这里插入图片描述
在这里插入图片描述

// 选择文件资源
var that=this;
uni.chooseMessageFile({
	count: 1,
	type: 'file',
	success(ress) {
		let type = ress.tempFiles[0].type
		// console.log(type)
		if (ress.tempFiles.length > 0 ) {
			if (ress.tempFiles[0].size < 8388608) {
				uni.showLoading({ title:'上传中' })
				let tempFilePaths = ress.tempFiles
                // 下载保存文件资源
 				uni.uploadFile({
					url: 'https://example.weixin.qq.com/upload', //仅为示例,非真实的接口地址
					filePath: tempFilePaths[0].path,
					name: 'file',
					header:{
						"token": uni.getStorageSync('token')||'',
					},
					formData: { "file": tempFilePaths[0].path },
					success: (res) => {
						var d=JSON.parse(res.data);
						that.toast(d.msg);
						if(d.code==1){
							uni.hideLoading();
						}
					}
				});
			} else {
				that.toast('超出限制大小');
				return
			}
		} else {
			that.toast('文件格式错误');
		}
	}
})
  • 1
    点赞
  • 2
    收藏
    觉得还不错? 一键收藏
  • 0
    评论
评论
添加红包

请填写红包祝福语或标题

红包个数最小为10个

红包金额最低5元

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

抵扣说明:

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

余额充值