简体   繁体   English

尝试将生成的PDF文件从React Js发送到Django-Rest Post请求时,未提交任何文件

[英]No file was submitted when try to send generated PDF file from React Js to Django-Rest Post request

Trying to generate pdf file in react js and then sending it to the django rest backend. 尝试在react js中生成pdf文件,然后将其发送到django rest后端。

I have successfully created the pdf file using jsPDF and html2canvas, but now unable to send to the rest api, whenever I submit it gives me response "No file was submitted".I have checked django-rest api's and its working fine, the pdf is not going to the rest api's.Here's my below code: 我已经使用jsPDF和html2canvas成功创建了pdf文件,但现在无法发送到其余的api,无论何时提交,它都会给我答复“未提交文件”。不去其余的api。这是我的下面的代码:

genPDF=(evt)=>{
    evt.preventDefault();
   html2canvas(document.getElementById("pdfid")).then(canvas=>{
       let img=canvas.toDataURL('img/png');
       let doc=new JsPDF();
       doc.addImage(img,'JPEG',30,30);
       //doc.save('test.pdf');
       let file=doc;
       const formdata=new FormData();
       formdata.append("file",file);
       this.postpdf(formdata)
   });

};

postpdf=(payload)=>{

    fetch(`http://127.0.0.1:8000/chauffeur/pdf_upload/`,
        {
            method: 'POST',
            body: JSON.stringify(payload),
            headers: {
                'Content-Type': 'application/json'
            }
        }
    ).then(response => response.json()).catch(error => console.error('Error:', error));
};

Request Headers 请求标题

       Content-Type: multipart/form-data; boundary=----                       
       WebKitFormBoundaryEueEwtpzbquHU6Tb
       Origin: http://localhost:3000
       Referer: http://localhost:3000/contractinvoice
       User-Agent: Mozilla/5.0 (Windows NT 10.0; Win64; x64) AppleWebKit/537.36 (KHTML, like Gecko)
       Chrome/73.0.3683.86 Safari/537.36

Response Headers 响应标题

       Access-Control-Allow-Credentials: true
       Access-Control-Allow-Origin: http://localhost:3000
       Allow: GET, POST, HEAD, OPTIONS
       Content-Length: 76
       Content-Type: application/json
       Date: Wed, 10 Apr 2019 05:44:49 GMT
       Server: WSGIServer/0.2 CPython/3.5.2
       Vary: Accept, Cookie, Origin
       X-Frame-Options: SAMEORIGIN

I think I am sending the file in a wrong but can't sort it what's the problem,need for suggestions.Thanks 我认为我发送的文件有误,但无法对问题进行排序,需要建议。谢谢

You have error here: 您在这里有错误:

'Content-Type': 'application/json'

If you want to send file, you should use multipart/form-data 如果要发送文件,则应使用multipart / form-data

why do you want to convert payload to JSON.stringify()... payload is not json... try this... 为什么要将有效负载转换为JSON.stringify()...有效负载不是json ...请尝试此...

postpdf=(payload)=>{

    fetch(`http://127.0.0.1:8000/chauffeur/pdf_upload/`,
        {
            method: 'POST',
            body: payload,
            headers: {
                'Content-Type': 'multipart/form-data'
            }
        }
    ).then(response => response.json()).catch(error => console.error('Error:', error));
};

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

相关问题 将PDF生成的文件发送到Django Rest - Send PDF generated file to Django Rest 当尝试使用django Rest框架和REACT与Axios上传文件时,如何修复“没有提交文件。” - How to fix “No file was submitted.” when trying to upload a file with django Rest framework and REACT with Axios 将图像发布到 django rest API 总是返回“未提交文件” - POST image to django rest API always returns 'No file was submitted' 将POST请求从js文件发送到php文件 - Send POST request from js file to php file 如何发送带有react和django休息的文件 - How to send file with react and django rest Django Rest Framework+React JS,无法实现表单解析器(错误:提交的数据不是文件。检查表单上的编码类型。) - Django Rest Framework+React JS ,unable to implement form parser (error: The submitted data was not a file. Check the encoding type on the form.) JS AJAX将生成的文件作为PDF发布到服务器目录 - JS AJAX to post generated file as PDF to server directory 通过ajax将pdf文件从PHP发送到JS - Send pdf file from PHP to JS by ajax 从 js 向 django rest 框架发出 post 请求 - Make post request from js to django rest framework 提交表单发布表单后,如果只能通过发布访问文件,如何将消息发送到html / javascript? 发布请求 - After form post form is submitted, how to send message to html/javascript if file can only be accessed through post? post request
 
粤ICP备18138465号  © 2020-2024 STACKOOM.COM