mirror of
https://gitee.com/dromara/electron-egg.git
synced 2026-05-14 19:52:10 +08:00
上传文件demo
This commit is contained in:
@@ -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;
|
||||
}
|
||||
|
||||
/**
|
||||
* 测试接口
|
||||
*/
|
||||
|
||||
Reference in New Issue
Block a user