-
Notifications
You must be signed in to change notification settings - Fork 4
Commit
This commit does not belong to any branch on this repository, and may belong to a fork outside of the repository.
Merge branch 'master' into patch-fix-tab
- Loading branch information
Showing
8 changed files
with
138 additions
and
3 deletions.
There are no files selected for viewing
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
Original file line number | Diff line number | Diff line change |
---|---|---|
|
@@ -2,5 +2,6 @@ | |
"editor.wordSegmenterLocales": null, | ||
"editor.language.brackets": [ | ||
|
||
] | ||
], | ||
"editor.detectIndentation": false | ||
} |
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
Original file line number | Diff line number | Diff line change |
---|---|---|
@@ -0,0 +1,14 @@ | ||
import fs from "fs/promises"; | ||
import { ApiCall } from "tsrpc"; | ||
import { ReqUpload, ResUpload } from "../shared/protocols/PtlUpload"; | ||
|
||
export async function ApiUpload(call: ApiCall<ReqUpload, ResUpload>) { | ||
// Write to file, or push to remote OSS... | ||
// 这里需要写实际的保存路径,在本地测试,加上/是绝对路径,不加是相对路径 | ||
// 注意,需要先创建 uploads 文件夹(已创建) | ||
await fs.writeFile('uploads/' + call.req.fileName, call.req.fileData); | ||
|
||
call.succ({ | ||
url: 'http://127.0.0.1:3000/uploads' + call.req.fileName | ||
}); | ||
} |
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
Original file line number | Diff line number | Diff line change |
---|---|---|
@@ -0,0 +1,8 @@ | ||
export interface ReqUpload { | ||
fileName: string, | ||
fileData: Uint8Array | ||
} | ||
|
||
export interface ResUpload { | ||
url: string; | ||
} |
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
Original file line number | Diff line number | Diff line change |
---|---|---|
@@ -0,0 +1 @@ | ||
此文件夹用来存放客户端上传的文件 |
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
Original file line number | Diff line number | Diff line change |
---|---|---|
@@ -0,0 +1,66 @@ | ||
<template> | ||
<div> | ||
<a-upload | ||
action="/" | ||
@before-upload="beforeUpload" | ||
:auto-upload="false" | ||
ref="uploadRef" | ||
> | ||
<template #upload-button> | ||
<a-space> | ||
<a-button> select file</a-button> | ||
|
||
</a-space> | ||
</template> | ||
</a-upload><a-button type="primary" @click="customRequest"> start upload</a-button> | ||
</div> | ||
</template> | ||
|
||
<script> | ||
import { client } from '@/client'; | ||
export default { | ||
data() { | ||
return { | ||
}; | ||
}, | ||
methods: { | ||
beforeUpload(file) { | ||
client.logger.info('beforeUpload', file); | ||
client.logger.info('beforeUpload', this.file); | ||
this.file = file; // 在这里获取到文件对象,并保存在data属性中 | ||
return false; // 阻止文件自动上传 | ||
}, | ||
async customRequest() { | ||
client.logger.info('customRequest', this.file); | ||
if (!this.file) { | ||
this.$message.error('Please select a file'); | ||
return; | ||
} | ||
let fileData = await this.loadFile(this.file); | ||
client.logger.info('fileData', fileData); | ||
let ret = await client.callApi('Upload', { | ||
fileData: fileData, | ||
fileName: this.file.name | ||
}); | ||
if (!ret.isSucc) { | ||
this.$message.error(ret.err.message); | ||
client.logger.error('Upload fail', ret.err); | ||
return; | ||
} | ||
client.logger.info('Upload successfully', ret.isSucc); | ||
this.$message.success('Upload successfully!'); | ||
}, | ||
loadFile(file) { | ||
return new Promise(rs => { | ||
let reader = new FileReader(); | ||
reader.onload = e => { | ||
rs(new Uint8Array(e.target.result)); | ||
} | ||
reader.readAsArrayBuffer(file); | ||
}) | ||
} | ||
} | ||
} | ||
</script> |