refactor: 使用表单进行文件上传!

* 可以上传大文件啦
* 最大限制 2GB
* 后端方法重置
This commit is contained in:
CrescentLeaf
2025-11-01 01:12:50 +08:00
parent dffa773acc
commit 8b3b32422f
7 changed files with 134 additions and 30 deletions

View File

@@ -116,6 +116,37 @@ class Client {
}
return re
}
static async uploadFileLikeApi(fileName: string, fileData: ArrayBuffer | Blob | Response, chatId?: string) {
const form = new FormData()
form.append("file",
fileData instanceof ArrayBuffer
? new File([fileData], fileName, { type: 'application/octet-stream' })
: (
fileData instanceof Blob ? fileData :
new File([await fileData.arrayBuffer()], fileName, { type: 'application/octet-stream' })
)
)
form.append('file_name', fileName)
chatId && form.append('chat_id', chatId)
const re = await fetch('./upload_file', {
method: 'POST',
headers: {
"Token": data.access_token,
"Device-Id": data.device_id,
} as HeadersInit,
body: form,
credentials: 'omit',
})
return {
...await re.json(),
code: re.status,
} as ApiCallbackMessage
}
static async uploadFile(fileName: string, fileData: ArrayBuffer | Blob | Response, chatId?: string) {
const re = await this.uploadFileLikeApi(fileName, fileData, chatId)
if (re.code != 200) throw new Error(re.msg)
return re.data!.hash as string
}
static async updateCachedProfile() {
this.myUserProfile = (await Client.invoke("User.getMyInfo", {
token: data.access_token

View File

@@ -167,12 +167,16 @@ export default function ChatFragment({ target, showReturnButton, onReturnButtonC
setIsMessageSending(true)
for (const fileName of Object.keys(cachedFiles.current)) {
if (text.indexOf(fileName) != -1) {
const re = await Client.invoke("Chat.uploadFile", {
/* const re = await Client.invoke("Chat.uploadFile", {
token: data.access_token,
file_name: fileName,
target,
data: cachedFiles.current[fileName],
}, 5000)
}, 5000) */
const re = await Client.uploadFileLikeApi(
fileName,
cachedFiles.current[fileName]
)
if (checkApiSuccessOrSncakbar(re, `文件[${fileName}] 上传失败`)) return setIsMessageSending(false)
text = text.replaceAll('(' + fileName + ')', '(tws://file?hash=' + re.data!.file_hash as string + ')')
}
@@ -225,16 +229,22 @@ export default function ChatFragment({ target, showReturnButton, onReturnButtonC
for (const file of files) {
addFile(file.type, file.name, file)
}
uploadChatAvatarRef.current!.value = ''
attachFileInputRef.current!.value = ''
})
useEventListener(uploadChatAvatarRef, 'change', async (_e) => {
const file = uploadChatAvatarRef.current!.files?.[0] as File
if (file == null) return
const re = await Client.invoke("Chat.setAvatar", {
let re = await Client.uploadFileLikeApi(
'avatar',
file
)
if (checkApiSuccessOrSncakbar(re, "上传失败")) return
const hash = re.data!.file_hash
re = await Client.invoke("Chat.setAvatar", {
token: data.access_token,
target: target,
avatar: file
file_hash: hash,
})
uploadChatAvatarRef.current!.value = ''

View File

@@ -21,14 +21,23 @@ export default function MyProfileDialog({
}: Refs) {
const editAvatarButtonRef = React.useRef<HTMLElement>(null)
const chooseAvatarFileRef = React.useRef<HTMLInputElement>(null)
useEventListener(editAvatarButtonRef, 'click', () => chooseAvatarFileRef.current!.click())
useEventListener(editAvatarButtonRef, 'click', () => {
chooseAvatarFileRef.current!.value = ''
chooseAvatarFileRef.current!.click()
})
useEventListener(chooseAvatarFileRef, 'change', async (_e) => {
const file = chooseAvatarFileRef.current!.files?.[0] as File
if (file == null) return
const re = await Client.invoke("User.setAvatar", {
let re = await Client.uploadFileLikeApi(
'avatar',
file
)
if (checkApiSuccessOrSncakbar(re, "上传失败")) return
const hash = re.data!.file_hash
re = await Client.invoke("User.setAvatar", {
token: data.access_token,
avatar: file
file_hash: hash
})
if (checkApiSuccessOrSncakbar(re, "修改失败")) return