-
Notifications
You must be signed in to change notification settings - Fork 0
Commit
This commit does not belong to any branch on this repository, and may belong to a fork outside of the repository.
- Loading branch information
wuwei
committed
Jun 25, 2024
1 parent
e9ef193
commit 7f3c73b
Showing
8 changed files
with
156 additions
and
0 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 |
---|---|---|
|
@@ -21,3 +21,4 @@ dist-ssr | |
*.sln | ||
*.sw? | ||
stats.html | ||
/public/uploads/ |
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
Some generated files are not rendered by default. Learn more about how customized files appear on GitHub.
Oops, something went wrong.
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,12 @@ | ||
import request from "@/utils/request"; | ||
|
||
export const fileUploadApi = (data) => { | ||
return request({ | ||
url: "/api/upload/single", | ||
method: "POST", | ||
data, | ||
headers: { | ||
"Content-Type": "multipart/form-data", | ||
}, | ||
}); | ||
}; |
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,37 @@ | ||
<template> | ||
<div class="file-upload"> | ||
<input ref="uploadRef" type="file" :multiple="multiple" :accept="accept" /> | ||
<button @click="handleUpload">上传</button> | ||
</div> | ||
</template> | ||
|
||
<script setup> | ||
import { ref } from "vue"; | ||
import { fileUploadApi } from "./api"; | ||
const props = defineProps({ | ||
multiple: { | ||
type: Boolean, | ||
default: true, | ||
}, | ||
accept: { | ||
type: Array, | ||
default: () => { | ||
return []; | ||
}, | ||
}, | ||
}); | ||
const uploadRef = ref(); | ||
const handleUpload = async () => { | ||
const file = uploadRef.value.files[0]; | ||
if (!file) { | ||
console.warn("请选择文件"); | ||
return; | ||
} | ||
const formData = new FormData(); | ||
formData.append("file", file); | ||
const res = await fileUploadApi(formData); | ||
console.log(res); | ||
}; | ||
</script> |
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,47 @@ | ||
import express from "express"; | ||
import multiparty from "multiparty"; | ||
|
||
const router = express.Router(); | ||
/** | ||
* Uploads a file using the multiparty library. | ||
* | ||
* @param {Object} req - The request object containing the file to be uploaded. | ||
* @return {Promise<Object>} A promise that resolves to an object containing the parsed fields and files. | ||
*/ | ||
const multipartyUpload = (req) => { | ||
return new Promise(async (resolve, reject) => { | ||
const form = new multiparty.Form({ | ||
maxFilesSize: 1024 * 1024 * 1024, | ||
}); | ||
form.uploadDir = "./public/uploads"; | ||
form.parse(req, (err, fields, files) => { | ||
if (err) { | ||
reject(err); | ||
return; | ||
} | ||
resolve({ fields, files }); | ||
}); | ||
}); | ||
}; | ||
|
||
router.post("/single", async (req, res) => { | ||
try { | ||
let { files } = await multipartyUpload(req); | ||
let file = (files.file && files.file[0]) || {}; | ||
res.send({ | ||
code: 0, | ||
message: "上传成功", | ||
data: { | ||
originalFilename: file.originalFilename, | ||
servicePath: file.path.replace("public", ""), | ||
}, | ||
}); | ||
} catch (error) { | ||
res.send({ | ||
code: 1, | ||
message: error.message, | ||
}); | ||
} | ||
}); | ||
|
||
export default router; |