上传文件demo

This commit is contained in:
gaoshuaixing
2022-11-17 15:32:10 +08:00
parent 5918e2853d
commit ba40375b06
7 changed files with 138 additions and 35 deletions

View File

@@ -495,27 +495,6 @@ class ExampleController extends Controller {
return;
}
/**
* 上传文件
*/
async uploadFile() {
// const self = this;
// const { ctx, service } = this;
// let tmpDir = Utils.getLogDir();
// const file = ctx.request.files[0];
// try {
// let tmpFile = fs.readFileSync(file.filepath)
// fs.writeFileSync(path.join(tmpDir, file.filename), tmpFile)
// } finally {
// await fs.unlink(file.filepath, function(){});
// }
// const fileStream = fs.createReadStream(path.join(tmpDir, file.filename))
// const uploadRes = await service.example.uploadFileToSMMS(fileStream);
// return uploadRes;
}
/**
* 检测http服务是否开启
*/
@@ -622,6 +601,27 @@ class ExampleController extends Controller {
}
}
/**
* 上传文件
*/
async uploadFile() {
let tmpDir = Utils.getLogDir();
const files = this.app.request.files;
let file = files.file;
let tmpFilePath = path.join(tmpDir, file.originalFilename);
try {
let tmpFile = fs.readFileSync(file.filepath);
fs.writeFileSync(tmpFilePath, tmpFile);
} finally {
await fs.unlink(file.filepath, function(){});
}
const fileStream = fs.createReadStream(tmpFilePath);
const uploadRes = await this.service.example.uploadFileToSMMS(fileStream);
return uploadRes;
}
/**
* 测试接口
*/