题目描述
就是文件要上传时用fileReader.readAsBinaryString读取为二进制,再把这它保存为Blob文件并下载这个操作,对于像图片,压缩文件等文件,重新下载后的文件会无法打开(.txt文件能够正确打开),有大神知道这是什么原因吗?
相关代码
<el-upload
id='upload'
class="file-field input-field"
drag
action="123"
:before-upload="beforeUpload"
:headers="header"
multiple>
<i class="el-icon-upload"></i>
<div class="el-upload__text">将文件拖到此处,或<em>点击上传</em></div>
<div class="el-upload__tip" slot="tip">上传文件,且不超过10Mb</div>
</el-upload>
beforeUpload (file) {
// let vm = this
let fileReader = new FileReader()
fileReader.readAsBinaryString(file)
fileReader.onload = function (e) {
let downloadFile = new Blob([e.target.result])
let url = window.URL.createObjectURL(downloadFile)
let link = document.createElement('a')
link.style.display = 'none'
link.href = url
link.setAttribute(
'download',
file.name
)
document.body.appendChild(link)
link.click()
}
return false
},
content-type
没了吧。传个type
更新时间: 2020年4月20日10:01:56
可以看到通过你的方法。出来的大小都不一样了。所以应是
toBlob
的时候转换有问题。https://www.lilnong.top/stati...