// 选择图片文件
	uni.chooseImage({
	  count: 1,
	  success: function (res) {
	    var tempFilePath = res.tempFilePaths[0];
		console.log(tempFilePath);
		
		
		// 将blob转换为base64字符串
		var xhr = new XMLHttpRequest();
		xhr.open('GET',tempFilePath);
		xhr.responseType = 'blob';
		xhr.onload = function () {
		  var blob = xhr.response;
		  var reader = new FileReader();
		  reader.readAsDataURL(blob);
		  reader.onloadend = function () {
		    var base64Str = reader.result;
		    console.log('base64Str', base64Str);
			// 上传图片文件到后端
			uni.uploadFile({
			  url: 'http://47.94.4.201/index.php/index/upload/uploadimg',
			  filePath: tempFilePath,
			  name: 'file',
			  formData: {
			    'imgurl':base64Str
			  },
			  success: function (uploadRes) {
			    console.log('upload success', uploadRes.data);
			  },
			  fail: function (err) {
			    console.log('upload fail', err);
			  }
			});
		  };
		};
		xhr.send();
	  }
	});

Logo

华为开发者空间,是为全球开发者打造的专属开发空间,汇聚了华为优质开发资源及工具,致力于让每一位开发者拥有一台云主机,基于华为根生态开发、创新。

更多推荐