javascript - 缺少请求的请求 token

标签 javascript ios react-native request multipartform-data

从电话簿中获取所有联系人并上传到服务器但出现以下错误。

虽然在请求正文中附加图像 FormData

enter image description here

尝试过的代码

传递文件 url 联系 thumbnailPath

const path = con.thumbnailPath
body.append('image', {
     uri: path,
     type: 'image/jpeg',
     name: 'photo.jpg',
     type: 'multipart/form-data'
})

尝试过的代码

传递文件 url 联系 thumbnailPath 没有 "file://"

const path = con.thumbnailPath.replace('file://', '')
body.append('image', {
     uri: path,
     type: 'image/jpeg',
     name: 'photo.jpg',
     type: 'multipart/form-data'
})

尝试过的代码

使用 react-native-fs 检查文件是否存在于路径中

if (con.thumbnailPath != '') {
     let isExist = RNFS.exists(con.thumbnailPath)
     if (isExist) {
         const path = con.thumbnailPath.replace('file://', '')
         console.log("Exist", path)
         body.append('image', {
             uri: path,
             type: 'image/jpeg',
             name: 'photo.jpg',
             type: 'multipart/form-data'
         })
     }
}

请求

fetch(url, {
    method: 'POST',
    headers: {
        'Authorization': token,
        'token': token
    },
    body: params 
})
.then((res) => res.json())
.then((json) => {
    console.log("RESPONSE:- ", json)
    if (json.response[0].status == 'false') {
        let msg = json.response[0].response_msg
        callback(new Error(msg), json.response[0])
    }
    else {
        callback(null, json.response[0])
    }
})
.catch((err) => {
    console.log(err)
    callback(err, null)
})

最佳答案

问题来自react-native@0.63.2的内部错误。

一个快速的解决方案是恢复这个提交:https://github.com/facebook/react-native/commit/31980094107ed37f8de70972dbcc319cc9a26339#diff-9a034658197479288c4d346a0eb4d98c

node_modules 中手动恢复此提交后,重新编译应用程序,图像上传将正常工作。

/Libraries/Image/RCTLocalAssetImageLoader.mm 中的函数 loadImageForURL 替换为以下内容:

 - (RCTImageLoaderCancellationBlock)loadImageForURL:(NSURL *)imageURL
                                           size:(CGSize)size
                                          scale:(CGFloat)scale
                                     resizeMode:(RCTResizeMode)resizeMode
                                progressHandler:(RCTImageLoaderProgressBlock)progressHandler
                             partialLoadHandler:(RCTImageLoaderPartialLoadBlock)partialLoadHandler
                              completionHandler:(RCTImageLoaderCompletionBlock)completionHandler
 {
   __block auto cancelled = std::make_shared<std::atomic<bool>>(false);
   RCTExecuteOnMainQueue(^{
     if (cancelled->load()) {
       return;
     }

     UIImage *image = RCTImageFromLocalAssetURL(imageURL);
     if (image) {
       if (progressHandler) {
         progressHandler(1, 1);
       }
       completionHandler(nil, image);
     } else {
       NSString *message = [NSString stringWithFormat:@"Could not find image %@", imageURL];
       RCTLogWarn(@"%@", message);
       completionHandler(RCTErrorWithMessage(message), nil);
     }
   });

   return ^{
     cancelled->store(true);
   };
 }

关于javascript - 缺少请求的请求 token ,我们在Stack Overflow上找到一个类似的问题: https://stackoverflow.com/questions/54663887/

相关文章:

javascript - 在mvc中播放.mp4视频时如何停止 slider ?

ios - Gmsmap 快速旋转 View

php - 如何从数组的输出中隐藏/显示 div

javascript - 'session' is undefined - Este JS/Express/Redis for session store

javascript - 我怎样才能从 d3js 中折叠的所有节点开始?

ios - 删除应用程序后,我创建的内存路径和其中的数据会发生什么变化?

ios - 收到远程通知时如何让我的声音播放?

react-native - null 不是对象(评估 'NativeUnimoduleProxy.viewManagersNames' )

reactjs - React Native Audio 将语音注释发送到后端

javascript - 标识符 'reducer' 已声明