简体   繁体   中英

Send PDF generated file to Django Rest

I Have generated a pdf of an html,

But as soon as pdf generated I want to mail it for this purpose i have to send it to django rest for backend, but could not sort how can I send it to django after the pdf generated.

   genPDF=(evt)=>{

    evt.preventDefault();
   html2canvas(document.getElementById("pdfid")).then(canvas=>{
       let img=canvas.toDataURL('img/png');
       let doc=new JsPDF();
       let image=doc.addImage(img,'JPEG',30,30);
       let formdata=new FormData();
       formdata.append('file');

       axios.post(`http://127.0.0.1:8000/chauffeur/pdf_upload/`,formdata);
   });

DJANGO PART:

class PdfUpload(APIView):

def get(self, request):
    return Response([PdfSerializer(file).data for file in Pdf.objects.all()])

def post(self,request):
   project_file = File(request.data.get('file'))
   print("File is",project_file)
   return Response()

When I tried to print file in django it print nothing, however request status is 200 OK.

The append method of formData accepts 3 parameters. name , value and filename(optional) . you are missing value property which can be any Blob such as file.

Based on the jsPDF docs you can do this:

let pdf_blob = doc.output('blob');
formdata.append('file', pdf_blob);

The technical post webpages of this site follow the CC BY-SA 4.0 protocol. If you need to reprint, please indicate the site URL or the original address.Any question please contact:yoyou2525@163.com.

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