最新消息:雨落星辰是一个专注网站SEO优化、网站SEO诊断、搜索引擎研究、网络营销推广、网站策划运营及站长类的自媒体原创博客

javascript - How to send pdf file from front-end to Nodejs server? - Stack Overflow

programmeradmin1浏览0评论

I have a pdf generated file on the front end of my application that I got using html2pdf plugin. I have a nodemailer server at my backed where I can attach files and send it to an email of my choice. Is there a way I can send the pdf that is generated in the front end to Nodejs? I am also using express

Edit: Based on your advice I did

 **On the Client side**
var element = document.getElementById('element-to-print');
const elem = document.getElementById('html');
html2pdf().from(element).toPdf().get('pdf').then(function (pdf) {
            window.open(pdf.output('bloburl'), '_blank');
            var formData = new FormData();
            formData.append("filename", pdf);
            axios.post('/upload',formData).then(res => { console.log(res) })
            // formData.append("uploadedFile", fileInputElement.files[0]);
            })

On express app

app.post('/upload', fileUpload(), function(req, res) {  
    const sampleFile = req.files.uploadedFile;
    // do something with file
    res.send('File uploaded');
  })

But I get this error ing from index.js

TypeError: Cannot read property 'uploadedFile' of null

I have a pdf generated file on the front end of my application that I got using html2pdf plugin. I have a nodemailer server at my backed where I can attach files and send it to an email of my choice. Is there a way I can send the pdf that is generated in the front end to Nodejs? I am also using express

Edit: Based on your advice I did

 **On the Client side**
var element = document.getElementById('element-to-print');
const elem = document.getElementById('html');
html2pdf().from(element).toPdf().get('pdf').then(function (pdf) {
            window.open(pdf.output('bloburl'), '_blank');
            var formData = new FormData();
            formData.append("filename", pdf);
            axios.post('/upload',formData).then(res => { console.log(res) })
            // formData.append("uploadedFile", fileInputElement.files[0]);
            })

On express app

app.post('/upload', fileUpload(), function(req, res) {  
    const sampleFile = req.files.uploadedFile;
    // do something with file
    res.send('File uploaded');
  })

But I get this error ing from index.js

TypeError: Cannot read property 'uploadedFile' of null

Share Improve this question edited Sep 3, 2018 at 1:42 greenn asked Sep 2, 2018 at 20:51 greenngreenn 3092 gold badges9 silver badges21 bronze badges
Add a ment  | 

3 Answers 3

Reset to default 3

Yes.

  1. Create and endpoint/route in your express app
  2. Use a http agent like superagent, request or axios in your client
  3. Use multipart form or something like FormData to create the data that is supposed to be sent.
  4. Post it to the url you created in express.
  5. Use middlewere such as express-fileupload or busboy to handle the attachment.

So in your client. You have something like

var formData = new FormData();
formData.append("filename", "My awesome file");
formData.append("uploadedFile", fileInputElement.files[0]);

Then you post that with something like Axios

axios.post('/upload',formData).then(res => { console.log(res) })

In your express app you do something like

const express = require('express');
const fileUpload = require('express-fileupload');
const app = express();

app.post('/upload', fileUpload(), function(req, res) {  
  const sampleFile = req.files.uploadedFile;
  // do something with file
  res.send('File uploaded');
})
<form method="post" enctype="multipart/form-data" action="/">
<div>
  <label for="profile_pic">Choose file to upload</label>
  <input type="file" id="profile_pic" name="profile_pic"
      accept=".pdf">
</div>
<div>
  <button>Submit</button>
</div>

For receive it at node you have to define a new route at the same path.

this works for me. Where 'SelectedFile' is the PDF. Using FormData to convert it into a sendable format to a node server. Sorry im also using axios incase its confusing.

const onFileUpload = () => {

const formData = new FormData();

formData.append("file", selectedFile);

client
  .postPdf("/pdf/upload-pdf", formData, false)

  .then((res) => {
    console.log("res", res.data);
    
  })

  .catch((err) => {
    console.error("Unable to upload PDF", err);
  });
};

// Axios

postPdf: (path, data, withToken = true) => {
const url = `${host}${path}`;

const token = localStorage.getItem(tokenKey);
let headers = {};

if (withToken) {
  headers["Authorization"] = `Bearer ${token}`;
}

return axios.post(url, data);
},
发布评论

评论列表(0)

  1. 暂无评论