对服务器的本机发布图像不存在所需的请求部分'文件'

问题描述 投票:0回答:1

从邮递员发送照片没有问题。

标题是--->'Content-Type':'application / x-www-form-urlencoded'

body是----> form-data,{file:image ..}

发送标头到x-www-form-urlencoded或多部分/表单数据不起作用。

(HTTP状态400 –错误请求)

请注意,API中没有图像容量限制。请查看屏幕截图以了解更多邮递员。

我整夜呆了几天。请帮助我。...

用我的代码

        let localUri = this.state.image; // <--- is image uri. 
        let filename = localUri.split('/').pop();


        let match = /\.(\w+)$/.exec(filename);
        let type = match ? `image/${match[1]}` : `image`;


        let formData = new FormData();

        formData.append('photo', { file: localUri, name: filename, type: type });

        return fetch(MY_SERVER, {
          method: 'POST',
          body: formData,
          headers: {
            'Content-Type':'application/x-www-form-urlencoded'
          },
        }).then((response) => response.text())
              .then((responseData) => {
                  console.log(responseData);

                  console.log('file',formData)
              })
              .done();  

错误消息中我认为找不到名为file的密钥。这是API问题吗?

HTTP Status 400 – Bad Request
Required request part 'file' is not present

POSTMAN

POSTMAN

react-native post postman image-uploading http-status-code-400
1个回答
0
投票

我认为错误在于此行代码:

formData.append('photo', { file: localUri, name: filename, type: type });

而不是尝试附加如下内容:

formData.append('file', localUri); formData.append('name', filename); formData.append('type', type);

© www.soinside.com 2019 - 2024. All rights reserved.