node 转二进制 图片
Posted qq735675958
tags:
篇首语:本文由小常识网(cha138.com)小编为大家整理,主要介绍了node 转二进制 图片相关的知识,希望对你有一定的参考价值。
‘use strict‘;
const Service = require(‘egg‘).Service;
const fs = require(‘fs‘);
const path = require(‘path‘);
const sendToWormhole = require(‘stream-wormhole‘);
class UploadService extends Service {
async index() {
const ctx = this.ctx;
const stream = await ctx.getFileStream();
const fileName = stream.filename;
let target = path.join(this.config.baseDir, `app/public/comfiles/${stream.filename}`);
const result = await new Promise((resolve, reject) => {
const remoteFileStream = fs.createWriteStream(target);
stream.pipe(remoteFileStream);
let errFlag;
remoteFileStream.on(‘error‘, err => {
errFlag = true;
sendToWormhole(stream);
remoteFileStream.destroy();
reject(err);
});
remoteFileStream.on(‘finish‘, async () => {
if (errFlag) return;
fs.readFile(target,function (err,bytesRead) {
if (err) throw err;
console.log(bytesRead)
})
resolve({ fileName, name: stream.fields.name });
});
});
return result;
}
}
module.exports = UploadService;
以上是关于node 转二进制 图片的主要内容,如果未能解决你的问题,请参考以下文章