接入教程
1. 访问GoFile官网获取API密钥
2. 使用POST请求上传文件至指定端点
3. 处理返回的文件链接和存储信息
4. 集成下载功能到您的应用中
使用Python上传文件到GoFile
python
import requests
url = 'https://api.gofile.io'
api_key = 'YOUR_API_KEY'
file_path = '/path/to/your/file.txt'
with open(file_path, 'rb') as f:
files = {'file': f}
headers = {'Authorization': f'Bearer {api_key}'}
response = requests.post(f'{url}/upload', files=files, headers=headers)
if response.status_code == 200:
print('Upload successful:', response.json())
else:
print('Upload failed:', response.text)
使用PHP上传文件到GoFile
php
<?php
$url = 'https://api.gofile.io';
$api_key = 'YOUR_API_KEY';
$file_path = '/path/to/your/file.txt';
$ch = curl_init();
curl_setopt($ch, CURLOPT_URL, $url . '/upload');
curl_setopt($ch, CURLOPT_POST, true);
curl_setopt($ch, CURLOPT_RETURNTRANSFER, true);
curl_setopt($ch, CURLOPT_HTTPHEADER, [
'Authorization: Bearer ' . $api_key
]);
$file = new CURLFile($file_path);
$post_fields = ['file' => $file];
curl_setopt($ch, CURLOPT_POSTFIELDS, $post_fields);
$response = curl_exec($ch);
if ($response === false) {
echo 'Error: ' . curl_error($ch);
} else {
$data = json_decode($response, true);
if (isset($data['status']) && $data['status'] == 'ok') {
echo 'Upload successful';
} else {
echo 'Upload failed';
}
}
curl_close($ch);
?>
使用JavaScript上传文件到GoFile
javascript
const url = 'https://api.gofile.io';
const apiKey = 'YOUR_API_KEY';
const fileInput = document.getElementById('fileInput'); // 假设有文件输入框
async function uploadFile(file) {
const formData = new FormData();
formData.append('file', file);
try {
const response = await fetch(`${url}/upload`, {
method: 'POST',
headers: {
'Authorization': `Bearer ${apiKey}`
},
body: formData
});
const result = await response.json();
if (response.ok) {
console.log('Upload successful:', result);
return result;
} else {
console.error('Upload failed:', result);
throw new Error(result.message || 'Upload failed');
}
} catch (error) {
console.error('Error uploading file:', error);
}
}
// 使用示例
// uploadFile(fileInput.files[0]);
常见问题
GoFile API是否真的完全免费?
是的,GoFile提供完全免费的API服务,无文件大小限制,无需支付任何费用即可上传和存储文件。
上传的文件会永久保存吗?
GoFile会尽量长期保存上传的文件,但作为免费服务,建议不要将其用作唯一备份。具体保存期限请参考官方政策。
如何获取API密钥?
访问GoFile官网,注册账户后,在用户面板的API部分可以生成和管理您的API密钥。
Aitishiku.com