// 小文件本地路径转化成File文件流
async handleClick () {
const { canceled, filePaths } = await remote.dialog.showOpenDialog({
properties: ['openFile']
})
if (!canceled) {
// filePaths[0] C://xxxxx/a.png
const { size } = fs.statSync(filePaths[0])
// 文件上传大小限制 524288000
if (size >= 524288000) {
this.$message.warning('上传文件不能超过500M')
} else {
// 文件本地路径
this.uploadPath = filePaths[0]
// 文件名
const name = path.basename(item)
// 此方法适应于小文件转换
const buffer = fs.readFileSync(this.uploadPath)
const blob = new Blob([buffer])
this.fileStream = new File([blob], name);
}
}
},
// 大文件本地路径转换成File文件流
// 文件基础信息(大文件,超过2G)
const file = {
size: xxxx,
localPath: 'c://xxxxx.test.txt',
}
// 大文件要通过分片读取
const chunkSize = 1024 * 1204 * 60 // 每片大小为 60M
// 计算块的数量
const numberOfChunks = Math.ceil(file.size/ chunkSize )
let start = 0
for (let i = 1; i <= numberOfChunks; i++) {
// 解决fs.createReadStream读取分段文件,总是会大1的问题
const end = Math.min(i * chunkSize, file.size) - 1
const readStream = fs.createReadStream(file.localPath, {
start,
end,
highWaterMark: this.chunkSize,
})
const chunkBuffer = []
readStream.on('data', (chunk) => {
chunkBuffer.push(chunk)
})
// 读取结束
readStream.on('end', (chunk) => {
const blob = new Blob(chunkBuffer)
const _chunkFile = new File([blob], item.name)
// 解决fs.createReadStream读取分段文件,总是会大1的问题
start = end + 1
})
}