DEV Community

Cover image for Practical Puppeteer: How to upload a file programatically

Practical Puppeteer: How to upload a file programatically

Sony AK on December 19, 2019

Hi everybody! Today I will share about how to upload file using Puppeteer. If you don't know about Puppeteer yet, here is the brief explanation. ...
Collapse
 
ashiqcseworld profile image
Ashiqur Rahman • Edited

can you please write an article about, how to, make a puppeteer extra plugin?

I want to make a plugin so that, whenever any interactions (click on a button, navigate to a page, etc..) it will take a screenshot automatically before and after the interaction. so that I don't have to call page.screenshot manually every time!

Collapse
 
sonyarianto profile image
Sony AK

Hi Ashiqur, wow nice use case, I still don't have any experience for writing plugin for Puppetter but I will note it for this nice use case.

Collapse
 
dynamitebud profile image
Rudra

hey, i am starting a new project with puppeteer. Do you want to work ?

Collapse
 
sovanyio profile image
Brian Bolton

FYI: this call should have an await

    // Sets the value of the file input to fileToUpload
    inputUploadHandle.uploadFile(fileToUpload);
Enter fullscreen mode Exit fullscreen mode

Just spent an hour debugging an automatic upload form due to this :P

Collapse
 
csandman profile image
Christopher Sandvik

This is great, except I'm stuck at one part. What would you do if there is no upload button, and the upload is normally automatically trigerred when the file is uploaded? For some reason nothing happens when I call the uploadFile function, I would expect it to continue automatically.

Collapse
 
sonyarianto profile image
Sony AK

Hi Chris,
Thanks, but sorry I am still don't understand your use case, do you have any sample of code or target web to do that? What do you mean by "no upload button". Basically we just target the <input type="file"> and call the uploadFile method for the file input selector. Tell me more and maybe I can help you.

Collapse
 
olizh profile image
Oliver Zhang • Edited

Hi Sony, first of all, thanks for a very useful post. This works perfect on my Mac. But when I deploy the code on Heroku, the file doesn't really upload. It looks like Pupeteer on Heroku couldn't find the file in the following line, thus the submit resulting in no file upload:

inputUploadHandle.uploadFile(fileToUpload);

The file exists in that path on Heroku's server because I'm able to send that file as an email attachment using Node Mailer. I'v been scratching my head for two days in a row. I wonder if you happen to have tried this on a Heroku app.

Collapse
 
sonyarianto profile image
Sony AK

Hi Oliver, sorry for super late reply, maybe file path is wrong? or try by using full path to the file.

Collapse
 
harshajayaweeraxhj profile image
harshajayaweeraXHJ

Thanks Sony, Great article!
Can you please look into this issue. i'm struggling with it and stuck for days now.

github.com/puppeteer/puppeteer/iss...

It is about uploading an image
Thanks in advance.

Collapse
 
sonyarianto profile image
Sony AK

Hi Harshajayaweera, sorry for the late reply, I think I miss this, already got solution for your problem?