我们在开发时下载图片或文件,地址基本上都是https的格式,下面来说一下后端返回base64的文件流,是如何下载的
必须把返回的流去掉这一部分:data:image/png;base64,否则下载不了
如我自己的流:
java
data:image/png;base64,iVBORw0KGgoAAAANSUhEUgAAAJYAAACgCAIAAABhQAIFAAACaklEQVR42u3aUXLCMAwEUO5/6fYKMNLKcvL2k4E08UsHsfHnTy7PxxIgFISCEKEgFISCEKEgFISCEKEgFISCEKEgFISCEKG8h/ATyK9/q3KeXZ+dXAeECBEiRLiXsPQ9/MXSVD5bec+GdUCIECFChPcR/vp1XRlnKsfvGjcm1wEhQoQIEb6LsOv1xHsQIkSIECHCk+NM18/nyVsQIUKECBG+i3Cy5u76qb7t9kKIECFChPcRTi7f5tftYEOIECHCdxFOJnHZlUfNG5cIIUKECBHOEybGljRV5ZiTIxhChAgRIryb8FQFnN4ulRidECJEiBDhcwi7Rp7KElSOmR5DBioChAgRIkS4tOY+tTWoi3Pyce7SR74IESJEiLCx5k4fp6vmXlhhI0SIECHCpYSJyjtx2ZO3xanHAAgRIkSIcNfzwlMVdtfWpq5KYXJkQ4gQIUKEZwgT40NiS1J6+1P6sTNChAgRItxLOLkEiUfHm6t5hAgRIkR4H2Hip3e6Ou+qudNjF0KECBEivIMwTV45/mRln6g7ECJEiBDh3YTpCji9rF1J/EsgRIgQIcK7CRtPvb2G7qrjh6twhAgRIkR4gDCdyhKn+RO3OEKECBEivI8w/ZU+uSWpcg6JLcIIESJEiPA+wsTYkq4LTtXWIVqECBEiRLiIcLJW3jCSdI1pCBEiRIgQYXUk6arLE9XBxFMBhAgRIkT4bMKucWOSuVJNIESIECHCuwnTt0LiHCavMXQtCBEiRIjwMOEtdfDkcexgQ4gQIcLnE8qqIEQoCAUhQkEoCAUhQkEoCAUhQkEoCAUhQkEoCAUhQrk0/1j3Eulg4ZAoAAAAAElFTkSuQmCC
java
downImg(){
var byte=this.codeImg.replace("data:image/png;base64,","");
const fs = uni.getFileSystemManager(); //全局唯一的文件管理器
var number = Math.random();
fs.writeFile({
filePath: wx.env.USER_DATA_PATH + '/code' + number + '.png',
data: byte,
encoding: "base64",
success(res) {
uni.saveImageToPhotosAlbum({ //保存为png格式到相册
filePath: wx.env.USER_DATA_PATH + '/code' + number + '.png',
success: function (res) {
uni.showToast({ title: "下载成功", icon: "success" });
},
fail: function (err) {
console.log(err)
}
})
}
})
},