-
Notifications
You must be signed in to change notification settings - Fork 0
/
upload.js
48 lines (41 loc) · 1.29 KB
/
upload.js
1
2
3
4
5
6
7
8
9
10
11
12
13
14
15
16
17
18
19
20
21
22
23
24
25
26
27
28
29
30
31
32
33
34
35
36
37
38
39
40
41
42
43
44
45
46
47
48
// pages/api/upload.js
import { NextApiRequest, NextApiResponse } from 'next';
import axios from 'axios';
import FormData from 'form-data';
import { v2 as cloudinary } from 'cloudinary';
// 配置 Cloudinary(如果需要)
cloudinary.config({
cloud_name: 'your_cloud_name',
api_key: 'your_api_key',
api_secret: 'your_api_secret',
});
export default async function upload(req: NextApiRequest, res: NextApiResponse) {
if (req.method !== 'POST') {
return res.status(405).json({ error: 'Method not allowed' });
}
const { file, key } = req.body;
if (!file || !key) {
return res.status(400).json({ error: 'Missing file or key' });
}
// 使用 FormData 构建请求体
const formData = new FormData();
formData.append('file', file);
formData.append('key', key);
try {
// 发送请求到图片上传API
const response = await axios.post('https://img.ypshidifu.cn/api/upload', formData, {
headers: {
...formData.getHeaders(),
},
});
// 处理响应
if (response.data.code === '200') {
return res.status(200).json(response.data);
} else {
return res.status(400).json({ error: response.data.msg });
}
} catch (error) {
console.error('Upload error:', error);
return res.status(500).json({ error: 'Server error' });
}
}