我正在尝试在 Next.js 应用程序中裁剪图像,将其发送到应用程序中的 API 路由,最后发送到应用程序外部的 API 端点。如果我绕过 API 路由,它可以正常工作,但在通过它时就不行了。图像数据不再正确且无法处理。
客户端 (Next.js) --> API 路由 (Next.js) --> API 端点(外部)
客户端 (Next.js) -fetch使用FormDataviaPOST
async function handleSave(image: Blob) {
const file = new File([image], 'avatar.png', { type: 'image/png' })
const data = new FormData()
data.append('imageFile', file)
const response = await fetch(`/api/users/${userId}/media/avatar`,
{
body: data,
method: 'POST',
}
)
// const response = await fetch (`https://localhost:1337/user/${userId}/media/avatar`, {
// method: 'POST',
// headers: {
// "Authorization": `Bearer <JWT token>`,
// …Run Code Online (Sandbox Code Playgroud)