文件上传
2023年06月16日
一、web
1.1 base64
1.2 formData
二、node
2.1 base64
2.2 formData
import fs from "fs";
import axios from "axios";
import formData from "form-data";
async function uploadImage(fileData){
const params = new formData();
params.append("type", "image/png");
params.append("path", `statics/images/`);
params.append("bucket", "cdn-cn-1303248253");
params.append("site", "腾讯云");
params.append("file", fileData.file);
return await axios.post(
"http://devops.umu.work/api/object_upload",
params,
{
headers: {
headers: {
Cookie: "session_id=1cd26e76-fe0c-41ed-8ec2-daa3664155d6",
},
},
}
);
}
async function upload(fileData){
const { type } = fileData;
if(type === "image"){
const result = await uploadImage(fileData);
console.log(result.data);
}
}
const fileData = {
type: "image",
file: fs.createReadStream(
"/Users/zhangwenqiang/Pictures/assessment-poster.png"
),
};
upload(fileData).catch((error)=>{
console.log(`上传失败, 失败原因为: ${error}`);
});