Improve UploadFile() API.

This commit is contained in:
Yang Luo
2021-08-14 15:58:12 +08:00
parent b7e0a4fe4e
commit 004282f970
5 changed files with 48 additions and 51 deletions

View File

@ -94,20 +94,21 @@ class ApplicationEditPage extends React.Component {
handleUpload(info) {
if (info.file.type !== "text/html") {
Setting.showMessage("error", i18next.t("provider:Please select a HTML file"))
return
Setting.showMessage("error", i18next.t("application:Please select a HTML file"));
return;
}
this.setState({uploading: true})
UserBackend.uploadFile("termsofuse", `${this.state.applicationName}/termsofuse`, info.file)
this.setState({uploading: true});
const fullFilePath = `termsOfUse/${this.state.application.owner}/${this.state.application.name}.html`;
UserBackend.uploadFile(this.state.application.owner, "termsOfUse", this.state.application.name, fullFilePath, info.file)
.then(res => {
if (res.status === "ok") {
Setting.showMessage("success", i18next.t("general:Upload success"))
this.updateApplicationField("termsOfUse", res.data)
Setting.showMessage("success", i18next.t("application:File uploaded successfully"));
this.updateApplicationField("termsOfUse", res.data);
} else {
Setting.showMessage("error", res.msg)
Setting.showMessage("error", res.msg);
}
}).finally(() => {
this.setState({uploading: false})
this.setState({uploading: false});
})
}

View File

@ -55,8 +55,9 @@ export const CropperDiv = (props) => {
return false;
}
// Setting.showMessage("success", "uploading...");
const userId = `${user.owner}/${user.name}`;
UserBackend.uploadFile("avatar", userId, blob)
const extension = image.substring(image.indexOf('/') + 1, image.indexOf(';base64'));
const fullFilePath = `avatar/${user.owner}/${user.name}.${extension}`;
UserBackend.uploadFile("admin", "avatar", "", fullFilePath, blob)
.then((res) => {
if (res.status === "ok") {
window.location.href = "/account";
@ -107,7 +108,7 @@ export const CropperDiv = (props) => {
>
<Col style={{margin: "0px auto 40px auto", width: 1000, height: 300}}>
<Row style={{width: "100%", marginBottom: "20px"}}>
<input style={{display: "none"}} ref={input => uploadButton = input} type="file" onChange={onChange}/>
<input style={{display: "none"}} ref={input => uploadButton = input} type="file" accept="image/*" onChange={onChange}/>
<Button block onClick={selectFile}>{i18next.t("user:Select a photo...")}</Button>
</Row>
<Cropper

View File

@ -76,10 +76,10 @@ export function getAffiliationOptions(url, code) {
}).then(res => res.json());
}
export function uploadFile(folder, subFolder, file) {
export function uploadFile(owner, tag, parent, fullFilePath, file) {
let formData = new FormData();
formData.append("file", file);
return fetch(`${Setting.ServerUrl}/api/upload-file?folder=${encodeURIComponent(folder)}&subFolder=${encodeURIComponent(subFolder)}`, {
return fetch(`${Setting.ServerUrl}/api/upload-file?owner=${owner}&tag=${tag}&parent=${parent}&fullFilePath=${encodeURIComponent(fullFilePath)}`, {
body: formData,
method: 'POST',
credentials: 'include',