-
Notifications
You must be signed in to change notification settings - Fork 0
/
index.html
43 lines (37 loc) · 1.01 KB
/
index.html
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
// pages/index.js
import { useState } from 'react';
export default function Home() {
const [file, setFile] = useState(null);
const handleSubmit = async (e) => {
e.preventDefault();
if (!file) return;
const formData = new FormData();
formData.append('file', file);
formData.append('key', process.env.YPAPI_KEY); // 确保 API_KEY 是环境变量
try {
const response = await fetch('/api/upload', {
method: 'POST',
body: formData,
});
const data = await response.json();
if (response.ok) {
alert('Upload successful!');
console.log(data);
} else {
alert('Upload failed: ' + data.error);
}
} catch (error) {
console.error('Error:', error);
alert('Upload error');
}
};
return (
<div>
<h1>Upload Image</h1>
<form onSubmit={handleSubmit}>
<input type="file" onChange={(e) => setFile(e.target.files[0])} />
<button type="submit">Upload</button>
</form>
</div>
);
}