egg上传文件问题

 async upload() {

const { ctx } = this;

const stream = await ctx.getFileStream();

const file_name = path.basename(stream.filename);

const upload_path = process.cwd() + '/app/public/files/' + file_name;

const writeStream = fs.createWriteStream(upload_path);

let result;

try {

result = await stream.pipe(writeStream);

} catch (err) {

await sendToWormhole(stream);

throw err;

}

ctx.body = {

url: upload_path,

fields: stream.fields,

};

}

为什么上传小体积文件时候没问题,上传比如几十KB的时候,接收到的图片不能全部展示,比如
这是用来上传的图片,

然后接受完之后变成这样的

回答

async upload() {

const { ctx } = this;

const stream = await ctx.getFileStream();

const file_name = path.basename(stream.filename);

const upload_path = process.cwd() + '/app/public/files/' + file_name;

const result = await new Promise((resolve, reject) => {

const remoteFileStream = fs.createWriteStream(upload_path);

stream.pipe(remoteFileStream);

let errFlag;

remoteFileStream.on('error', err => {

errFlag = true;

remoteFileStream.destroy();

reject(err);

});

remoteFileStream.on('finish', async () => {

if (errFlag) return;

resolve({ fileName, name: stream.fields.name });

});

});

return result;

}

以上是 egg上传文件问题 的全部内容, 来源链接: utcz.com/a/33029.html

回到顶部