简体   繁体   English

尝试将 PDF 文件上传到 React-native 中的服务器时出错

[英]Get error when try to uplaod PDF file to server in React-native

I take PDF file from Phone Gallery and upload to server.我从电话库中获取 PDF 文件并上传到服务器。 but when I upload it to server.但是当我将它上传到服务器时。 I get a Error "[SyntaxError: JSON Parse error: Unrecognized token '<']".我收到错误“[SyntaxError: JSON Parse error: Unrecognized token '<']”。

my code Is:我的代码是:

  ```  const UploadCV= async()=>{ //Take pdf file from Phone.
      try {
        
        const res = await DocumentPicker.pick({
          type: [DocumentPicker.types.pdf],
        });
        setResume(res.uri)
       
        Alert.alert('Congrulation!!',"Your CV has been Successfuly Uploaded")
        setTimeout(() => {
          this.bs.current.snapTo(1);
        }, 2000);
      
       
      } catch (err) {
        if (DocumentPicker.isCancel(err)) {
       
        } else {
          throw err;
        }
      }
    }
       //Upload to Server
       const ApplyforJOb= async()=>{ 
         await fetch('https://thejoblocator.co.uk/api/RestApplyJob',{
           method:'post',
           headers:{
             'Content-Type':'application/octet-stream',
           },
            body: JSON.stringify({
              job_id:jobid,
              description:description,
              resume :resume                  
            })
         }).then(res=>res.json())
           .then(data=>{
             console.log("coming data", data)
           }).catch(err=>{
             console.log("Apply Job Error", err)
           })
       }```

You are not uploading any files Your error is saying that your json at body is not correct您没有上传任何文件您的错误是说您的 json 在正文不正确

The pdf file uri is link to assets in phone not the file and you can't upload a file in json like this,you can either make at blob and embed it like a string json and then upload or you can upload view form data Please check How to upload file to server using react-native pdf 文件 uri 链接到手机中的资产而不是文件,您不能像这样在 json 中上传文件,您可以在 blob 中制作并像字符串一样嵌入它 Z466DEEC76ECDF5FCA6 Please548571F63Z检查如何使用 react-native 将文件上传到服务器

声明:本站的技术帖子网页,遵循CC BY-SA 4.0协议,如果您需要转载,请注明本站网址或者原文地址。任何问题请咨询:yoyou2525@163.com.

 
粤ICP备18138465号  © 2020-2024 STACKOOM.COM