在较新的iOS版本中,使用form-data w/ multer和react-native-image-picker时出错([_NSCFConstantString substringToIndex:



当试图将包含图像的表单数据发送到后端的节点复用器库时,iOS模拟器在较新的iOS版本中给了我以下错误(13.7版本在发送图像时没有任何问题(:

Exception '*** -[__NSCFConstantString substringToIndex:]: Index 3 out of bounds; string length 0' was thrown while invoking sendRequest on target Networking with params (
{
data =         {
formData =             (
{
fieldName = name;
headers =                     {
"content-disposition" = "form-data; name="name"";
};
string = "User";
},
{
fieldName = email;
headers =                     {
"content-disposition" = "form-data; name="email"";
};
string = "user@gmail.com";
},
{
fieldName = topic;
headers =                     {
"content-disposition" = "form-data; name="topic"";
};
string = "New Calc Request";
},
{
fieldName = comment;
headers =                     {
"content-disposition" = "form-data; name="comment"";
};
string = Test;
},
{
fieldName = attachment;
headers =                     {
"content-disposition" = "form-data; name="attachment"; filename=""";
"content-type" = "";
};
name = "";
type = "";
uri = "";
}
);
trackingName = unknown;
};

这个问题似乎源于没有在附件中使用JSON.stringify。然而,当我使用它时,我似乎无法解析服务器上的图像附件供multer库使用。(注意:错误中的附件信息为空,因为模拟器在13之后的ios版本中不能很好地与M1芯片配合使用(。

正在创建的表单数据如下:

userFeedBackInputs.append('name', this.currentUser.givenName + ' ' + this.currentUser.surname);
userFeedBackInputs.append('email', this.currentUser.email);
userFeedBackInputs.append('topic', this.state.topic.value);
userFeedBackInputs.append('comment', (this.state.comment.value : ''));
userFeedBackInputs.append('attachment', {
uri: this.state.imageSrc.value,
name: this.state.imageSrc.fileName,
type: this.state.imageSrc.type
}
)

我如何a(在不使用JSON.stringify的情况下发送数据,或者b(在表单数据到达multer库之前解析表单数据中附件的JSON。(通常的节点体解析器似乎不适用于嵌套的JSON,因为这是一个多表单请求(

当我用运行ios 13的ios设备测试我的应用程序时,我遇到了完全相同的问题,我找到了解决方案。

原因是附件没有数据,只需记录"this.state.imageSrc.value"this.state.imageSrc.fileName"this.state.imageSrc.type",您会看到imageSrc为空,可能存在与文件选取器库有关的错误

如果您正在使用"react native image crop picker",请将其更新为0.33.3,然后删除Node_modules、Pods并重新运行,问题将得到解决

更多详细信息:https://github.com/ivpusic/react-native-image-crop-picker/issues/1130

最新更新