diff --git a/cloudflare-page/OneAPI-imgbed-MIX.html b/cloudflare-page/OneAPI-imgbed-MIX.html
index 64fcb14..1d04663 100644
--- a/cloudflare-page/OneAPI-imgbed-MIX.html
+++ b/cloudflare-page/OneAPI-imgbed-MIX.html
@@ -74,7 +74,7 @@
File Upload
-
+
diff --git a/cloudflare-worker-js-api/API_IMG_3001.js b/cloudflare-worker-js-api/API_IMG_3001.js
index 1cc0077..ee58e06 100644
--- a/cloudflare-worker-js-api/API_IMG_3001.js
+++ b/cloudflare-worker-js-api/API_IMG_3001.js
@@ -1,3 +1,9 @@
+/*
+ 这个试验我放在了KV库
+ 你需要创建和绑定名为 WORKER_IMGBED 的库,其中新建 K 字段,名为 3001_auth 然后在V中复制进去浏览器F12得到的完整Authorization即可
+ Authorization需要你注册登录 https://www.freebuf.com/ 获取,有效期不详,也许是一个月
+*/
+
addEventListener('fetch', event => {
event.respondWith(handleRequest(event.request))
})
@@ -7,23 +13,23 @@ addEventListener('fetch', event => {
* @param {Request} request
*/
async function handle3001Request(request) {
- // 允许跨域请求
+ // 定义 CORS 头部
const corsHeaders = {
'Access-Control-Allow-Origin': '*', // 根据需要调整
'Access-Control-Allow-Methods': 'POST, OPTIONS',
'Access-Control-Allow-Headers': 'Authorization, Content-Type',
}
+ // 处理 CORS 预检请求
if (request.method === 'OPTIONS') {
- // 处理 CORS 预检请求
return new Response(null, {
status: 204,
headers: corsHeaders,
})
}
+ // 处理 GET 请求,返回说明信息
if (request.method === 'GET') {
- // 返回简单的说明页面或提示信息
return new Response('3001 图床服务,请使用 POST 方法上传图片。', {
status: 200,
headers: {
@@ -33,13 +39,20 @@ addEventListener('fetch', event => {
})
}
+ // 仅处理 POST 请求
if (request.method === 'POST') {
try {
// 解析表单数据
const formData = await request.formData()
- const imageFile = formData.get('file')
+ const imageFile = formData.get('image') // 前端发送的是 'image'
const isBase64 = formData.get('is_base64') || '0'
+ // 打印所有表单字段(调试用)
+ for (const [key, value] of formData.entries()) {
+ console.log(`Form field: ${key}`)
+ }
+
+ // 校验图片文件
if (!imageFile || imageFile.size === 0) {
return new Response('No image file found in the request', {
status: 400,
@@ -63,7 +76,7 @@ addEventListener('fetch', event => {
})
}
- // 构建上传表单数据
+ // 构建上传表单数据,将 'image' 重命名为 'file'
const uploadFormData = new FormData()
uploadFormData.append('file', imageFile, imageFile.name)
uploadFormData.append('is_base64', isBase64)
@@ -91,9 +104,22 @@ addEventListener('fetch', event => {
body: uploadFormData,
})
- // 检查响应状态码
+ // 处理响应
if (response.status === 200) {
- const responseData = await response.json()
+ let responseData
+ try {
+ responseData = await response.json()
+ } catch (parseError) {
+ console.error('Error parsing JSON response:', parseError)
+ return new Response('上传失败:无法解析服务器响应。', {
+ status: 500,
+ headers: {
+ 'Content-Type': 'text/plain;charset=UTF-8',
+ ...corsHeaders,
+ },
+ })
+ }
+
console.log('响应 JSON 内容:', JSON.stringify(responseData, null, 2))
// 检查响应结构
@@ -107,31 +133,51 @@ addEventListener('fetch', event => {
console.log(`图片上传成功: ${imageUrl}`)
- // 返回成功响应
- return new Response(JSON.stringify({ imageUrl: imageUrl }), {
+ // 返回纯文本格式的图片 URL
+ return new Response(imageUrl, {
status: 200,
headers: {
- 'Content-Type': 'application/json;charset=UTF-8',
+ 'Content-Type': 'text/plain;charset=UTF-8',
...corsHeaders,
},
})
} else {
console.error(`上传失败,消息: ${responseData.msg || '未知错误'}`)
- return new Response(`上传失败,消息: ${responseData.msg || '未知错误'}`, {
+
+ // 返回原始 JSON 错误信息
+ return new Response(JSON.stringify(responseData), {
status: 500,
headers: {
- 'Content-Type': 'text/plain;charset=UTF-8',
+ 'Content-Type': 'application/json;charset=UTF-8',
...corsHeaders,
},
})
}
} else {
- const errorText = await response.text()
- console.error(`上传失败。状态码: ${response.status}, 响应内容: ${errorText}`)
- return new Response(`上传失败。状态码: ${response.status}`, {
+ // 尝试解析错误响应为 JSON
+ let errorData
+ try {
+ errorData = await response.json()
+ } catch {
+ // 如果解析失败,获取纯文本
+ const errorText = await response.text()
+ console.error(`上传失败。状态码: ${response.status}, 响应内容: ${errorText}`)
+ return new Response(`上传失败。状态码: ${response.status}`, {
+ status: response.status,
+ headers: {
+ 'Content-Type': 'text/plain;charset=UTF-8',
+ ...corsHeaders,
+ },
+ })
+ }
+
+ console.error(`上传失败。状态码: ${response.status}, 响应内容: ${JSON.stringify(errorData)}`)
+
+ // 返回原始 JSON 错误信息
+ return new Response(JSON.stringify(errorData), {
status: response.status,
headers: {
- 'Content-Type': 'text/plain;charset=UTF-8',
+ 'Content-Type': 'application/json;charset=UTF-8',
...corsHeaders,
},
})
diff --git a/cloudflare-worker-js-api/worker.js b/cloudflare-worker-js-api/worker.js
index 0941a70..09ccedc 100644
--- a/cloudflare-worker-js-api/worker.js
+++ b/cloudflare-worker-js-api/worker.js
@@ -468,23 +468,23 @@ async function handleRequest(request) {
}
async function handle3001Request(request) {
- // 允许跨域请求
+ // 定义 CORS 头部
const corsHeaders = {
'Access-Control-Allow-Origin': '*', // 根据需要调整
'Access-Control-Allow-Methods': 'POST, OPTIONS',
'Access-Control-Allow-Headers': 'Authorization, Content-Type',
}
+ // 处理 CORS 预检请求
if (request.method === 'OPTIONS') {
- // 处理 CORS 预检请求
return new Response(null, {
status: 204,
headers: corsHeaders,
})
}
+ // 处理 GET 请求,返回说明信息
if (request.method === 'GET') {
- // 返回简单的说明页面或提示信息
return new Response('3001 图床服务,请使用 POST 方法上传图片。', {
status: 200,
headers: {
@@ -494,13 +494,20 @@ async function handleRequest(request) {
})
}
+ // 仅处理 POST 请求
if (request.method === 'POST') {
try {
// 解析表单数据
const formData = await request.formData()
- const imageFile = formData.get('file')
+ const imageFile = formData.get('image') // 前端发送的是 'image'
const isBase64 = formData.get('is_base64') || '0'
+ // 打印所有表单字段(调试用)
+ for (const [key, value] of formData.entries()) {
+ console.log(`Form field: ${key}`)
+ }
+
+ // 校验图片文件
if (!imageFile || imageFile.size === 0) {
return new Response('No image file found in the request', {
status: 400,
@@ -524,7 +531,7 @@ async function handleRequest(request) {
})
}
- // 构建上传表单数据
+ // 构建上传表单数据,将 'image' 重命名为 'file'
const uploadFormData = new FormData()
uploadFormData.append('file', imageFile, imageFile.name)
uploadFormData.append('is_base64', isBase64)
@@ -552,9 +559,22 @@ async function handleRequest(request) {
body: uploadFormData,
})
- // 检查响应状态码
+ // 处理响应
if (response.status === 200) {
- const responseData = await response.json()
+ let responseData
+ try {
+ responseData = await response.json()
+ } catch (parseError) {
+ console.error('Error parsing JSON response:', parseError)
+ return new Response('上传失败:无法解析服务器响应。', {
+ status: 500,
+ headers: {
+ 'Content-Type': 'text/plain;charset=UTF-8',
+ ...corsHeaders,
+ },
+ })
+ }
+
console.log('响应 JSON 内容:', JSON.stringify(responseData, null, 2))
// 检查响应结构
@@ -568,31 +588,51 @@ async function handleRequest(request) {
console.log(`图片上传成功: ${imageUrl}`)
- // 返回成功响应
- return new Response(JSON.stringify({ imageUrl: imageUrl }), {
+ // 返回纯文本格式的图片 URL
+ return new Response(imageUrl, {
status: 200,
headers: {
- 'Content-Type': 'application/json;charset=UTF-8',
+ 'Content-Type': 'text/plain;charset=UTF-8',
...corsHeaders,
},
})
} else {
console.error(`上传失败,消息: ${responseData.msg || '未知错误'}`)
- return new Response(`上传失败,消息: ${responseData.msg || '未知错误'}`, {
+
+ // 返回原始 JSON 错误信息
+ return new Response(JSON.stringify(responseData), {
status: 500,
headers: {
- 'Content-Type': 'text/plain;charset=UTF-8',
+ 'Content-Type': 'application/json;charset=UTF-8',
...corsHeaders,
},
})
}
} else {
- const errorText = await response.text()
- console.error(`上传失败。状态码: ${response.status}, 响应内容: ${errorText}`)
- return new Response(`上传失败。状态码: ${response.status}`, {
+ // 尝试解析错误响应为 JSON
+ let errorData
+ try {
+ errorData = await response.json()
+ } catch {
+ // 如果解析失败,获取纯文本
+ const errorText = await response.text()
+ console.error(`上传失败。状态码: ${response.status}, 响应内容: ${errorText}`)
+ return new Response(`上传失败。状态码: ${response.status}`, {
+ status: response.status,
+ headers: {
+ 'Content-Type': 'text/plain;charset=UTF-8',
+ ...corsHeaders,
+ },
+ })
+ }
+
+ console.error(`上传失败。状态码: ${response.status}, 响应内容: ${JSON.stringify(errorData)}`)
+
+ // 返回原始 JSON 错误信息
+ return new Response(JSON.stringify(errorData), {
status: response.status,
headers: {
- 'Content-Type': 'text/plain;charset=UTF-8',
+ 'Content-Type': 'application/json;charset=UTF-8',
...corsHeaders,
},
})
diff --git a/python-uploader/test-3001.py b/python-uploader/test-3001.py
index b742be5..9a6a960 100644
--- a/python-uploader/test-3001.py
+++ b/python-uploader/test-3001.py
@@ -1,3 +1,7 @@
+"""
+ Authorization需要你注册登录 https://www.freebuf.com/ 获取,有效期不详,也许是一个月
+"""
+
import requests
import os
import json