Business
Jobs
  • About Us
  • Solutions
    • Job Postings
      Post your job and receive qualified candidates in 48h.
    • Candidate Assessments
      500+ technical and psychological tests, plus anti-fraud.
    • Headhunting
      Tailor-made executive search from start to finish.
    • Payroll + EOR
      Payroll dispersal and EOR across 15+ LATAM countries.
  • Pricing
  • Jobs

0

397
Views
Envíe un correo electrónico adjunto en PDF usando JsPDF con ajax y Django python

Tengo un problema con el envío de un correo electrónico adjunto en pdf usando jspdf con ajax y django. El problema es que las vistas del lado del servidor.py print (pdf_s) siempre vuelven a ejecutar 'Ninguno' pero el lado del cliente console.log (pdf) devuelve valores binarios.

Consulte el siguiente código que probé.

guión.js

Este es el código del lado del cliente. y estoy usando Ajax para enviar datos al servidor

 function sendMail(){ getCanvas().then(function(canvas) { console.log('Test'); var img = canvas.toDataURL("image/png",0.98); var imgWidth = 200; var pageHeight = 295; var imgHeight = canvas.height * imgWidth / canvas.width; var heightLeft = imgHeight; var doc = new jsPDF('p', 'mm', 'a4', 'pt'); var position = 0; doc.addImage(img, 'JPEG', 5, position, imgWidth, imgHeight); heightLeft -= pageHeight; while (heightLeft >= 0) { position = heightLeft - imgHeight; doc.addPage(); doc.addImage(img, 'JPEG', 5, position, imgWidth, imgHeight); heightLeft -= pageHeight; } var pdf = btoa(doc.output()); console.log(pdf); $.ajax({ type:'POST', url:'sendmail/', data:{ pdf_data: pdf, csrfmiddlewaretoken: csrftoken }, success:function(data){ console.log(data); } }); }); }

vistas.py

 def SendMail(request,scan_id): pdf_s = request.GET.get('pdf_data') print(pdf_s) fo = open('test.pdf','w') fo.write(pdf_s) fo.close() html_content = "Test Message" email = EmailMessage("test", html_content, "test", ["test@test.com"]) email.content_subtype = "html" fd = open('test.pdf', 'r') email.attach('test', fd.read(), 'application/pdf') res = email.send() if res: status = 'Success' else: status = 'Fail' return HttpResponse(status)
over 4 years ago · Santiago Trujillo
1 answers
Answer question

0

Verifique la solicitud de ajax a través de las herramientas de desarrollo del navegador> red. Quizás el parámetro "pdf_data" no existe.

Envío el archivo a través de js nativo.

JS

 var formData = new FormData(); formData.append('file.pdf', file); var xhr = new XMLHttpRequest(); xhr.onload = function (e) { if (xhr.readyState === 4 && xhr.status === 200){ result=xhr.responseText; console.log(result); } } xhr.open('POST', url, true); xhr.setRequestHeader("X-CSRFToken", csrfmiddlewaretoken); xhr.send(formData);

Pitón

 element_file=File(request.FILES[request.FILES.keys()[0]]) with open('file.pdf','wb+') as destination: for chunk in element_file.chunks(): destination.write(chunk)

Funciona en caso de que envíe un archivo desde una computadora local a través de tipo de entrada = "archivo"

over 4 years ago · Santiago Trujillo Report
Answer question
Find remote jobs

Discover the new way to find a job!

Top jobs
Top job categories
Business
Post vacancy Pricing Sales
Legal
Terms and conditions Privacy policy
© 2026 PeakU Inc. All Rights Reserved.
Andres GPT
Show me some job opportunities
There's an error!