uniapp 通过临时路径获取file文件

22,631 阅读1分钟

chooseImage API 可以获取到临时路径 如果想要获取到file文件怎么办呢,网上搜索一圈找到了解决办法

// 先拿到临时路径
addImg(e) {
	let that = this;
	uni.chooseImage({
		count: 1, //默认9
        	sizeType: ['original', 'compressed'], 
        	success: function(res) {
        		that.imgUrl = res.tempFilePaths[0];
        	        that.objectURLToBlob(res.tempFilePaths[0]).then(res => {
        	            that.ImgFile = res;
        		 });      		
        	}       	
	});     
},
//BlobUrl转blob数据
objectURLToBlob(blodurl) {
	uni.showLoading({
		title: '压缩中...'
	});
	return new Promise((resolve, reject) => {
		var http = new XMLHttpRequest();
		http.open('GET', blodurl, true);
		http.responseType = 'blob';
		http.onload = function(e) {
		    if (this.status == 200 || this.status === 0) {
			// console.log('blod数据',this.response);
			// 在将blod数据转为file
			let files = new window.File([this.response], 'file.name', { type: 'image' });
			// console.log('blod数据转换file',files);
			resolve(files);
			uni.hideLoading();
	            }
		};
		http.send();
	});
},

但是还有一些问题没有解决 创建file文件时候怎么把file.name 和 type 赋予正确的值呢,要大神知道的话请指教!