How to download a file generated "on the fly" with Puppeteer? - puppeteer

I want to download and rename a PDF file that is generated "on the fly" by an Angular app.
With this code, the PDF file is downloaded but I want to rename it before writing to disk.
//Download PDF
await secondTab.waitForSelector('img[title="Format PDF"]', { visible: true, timeout : 10000 })
await secondTab.waitForTimeout(10000);
var current_element = await secondTab.$('img[title="Format PDF"]');
var parent_node = await current_element.getProperty('parentNode');
await parent_node.click();
console.log("Downloading PDF");
I wasn't able to find a solution. All solutions talk about files downloaded from URL.
I tried to trace events on the page (cf. PageEventObjet), but no one catch this "on the fly" generated PDF file.
My question : is there a way to catch that stream, name it and save it to disk ?
Thank you very much.
Note : I dont want to play with the File system (i.e. find my downloaded PDF, rename it, move it, etc.)

Related

Will webViewLink generated for a file in appdata folder work?

I have stored a pdf file to appDataFolder which is application specific, using the get call I am trying to get the file url using 'webViewLink, webContentLink'. But opening the links does not show anything but using alt=media I was able to get the buffer and write it to a file.
const test = await drive.files.get({
fileId: fileId,
fields: 'webViewLink, webContentLink',
})

use local JSON file with react and electron

so I have created an electron App with the help of react.js.
The app should mainly display charts, for which I use chart.js. The data of the chart files need to be updated monthly, since I get new data every month from another department per Excel, which I quickly export to a JSON. Obviously I don't want to create a new production electron app every month. So is it possible to retrieve data from json files after production? I can't find anything on the web unfortunately.
How do I go about that, since I can't access JSON files outside of my src folder with react or can I?
thanks in advance!!
Electron is for creating desktop apps so you have full access to the file system.
To read a local file you can install and use Node fs. fs supports Promises, callbacks and synchronous methods.
Rather than hardcode the filepath, you probably want to do some friendly UI stuff like on first launch, ask the user to select the file they want to load and then store that path as a preference. Then on subsequent launches, test if the file exists and load it, otherwise alert the user to pick another file, etc.
let thePath = "/Users/Spring/Desktop/ticket.txt"
var fs = require('fs');
fs.readFile(thePath, 'utf8', function (err, data) {
if (err) return console.log(err);
// data is the contents of the text file we just read
});

Puppeteer: how to rename a file before downloading?

I want to crawl some file using puppeteer, and I find the downloading practice like below:
puppeteer - how to set download location
await page._client.send('Page.setDownloadBehavior', {
behavior: 'allow',
downloadPath: dirname,
});
but I want to change the filename now, some examples use "fs" module to rename it when download finished, but usually, there are many files and different names, it couldn't check it in PC file system manually, we want to click the download button and rename it at right, how can I reach it?

AS3: Download a download embedded file to desktop without browser

I'm creating an flash app where users can select something and download a temple. I'm publishing this file using air application with runtime embedded. In the app I've included a folder called documents with the individual files the user can download. Currently I'm using navigateToURL but I don't want it to rely on the browser. I've also tried this:
function surveyDownload(evt:MouseEvent):void {
var request = new URLRequest("document/template.docx");
var localRef = new FileReference();
try
{
// Prompt and download file
localRef.download( request );
}
catch (error:Error)
{
trace("Unable to download file.");
}
}
but all I get is the trace statement "Unable to download file".
How can I download an embedded file without the browser?
Your question is a little unclear. You want the user to download something from a server? If so then document/template.docx is not a URL so of course that will not work.
If you are talking about copying a file out of the AIR app bundle to the user's hard drive then you don't need URLRequest but rather the methods in the File class (browseForSave and copyTo).
Read the docs for File and search out some tutorials – there are loads and more complete than I would write here.

Download files from folder with webmatrix

I have a folder created to get the files that users upload. However when i try to download the files, some extensions don't download.
I have this code to list all files that are in the folder, i can see all the files with no problem.
#foreach (string fullFilePath in Directory.GetFiles(Path.Combine(Server.MapPath("~/uploadedFiles"),"Ticket Id - "+#id)))
{
<div class="linkFicheiros">#Path.GetFileName(fullFilePath)</div>
}
With this line
#Path.GetFileName(fullFilePath)
i can download files with extension: ".zip" , ".xls" but extensions like ".msg"(sometimes users need to upload this extension) i got an error "The page cannot be found". Even ".jpg" instead of downloading the file it opens the image on the browser.
I think that is something how i'm trying to reach the file, but i can't get to a solution.
Any thoughts ?
The browser would try to always view the content of the file. If its an Image file like .jng etc. But if there is a .zip file, it would let the user download and open it. Because browser can't open it.
You need to push the file to the user. For that you can try the following code:
var file = Server.MapPath("~/images/" + Request["img"]);
Response.AppendHeader(
"content-disposition", "attachment; filename=" +
Request["img"]);
Response.ContentType = "application/octet-stream";
Response.TransmitFile(file);
Now, you can see that in the code I am sharing, the file is a variable, which is being pointed to the file in the File System. Please note that there is a Query String parameter, which would be sent alongwith the URL, for example:
Download Image
Now the header would be appened, and the
Request.ContentType = "application/octet-stream"
is used to force the browser to show the Dialoug of Open/Save.
Then the transmit file to download it.
To only execute the code when needed
To only execute that download code block, you need to set the value in a block for condition to be true. For example, you can use a parameter to check whether to download the file or not. Try something simple like,
<a href="~/download_file/image_link.png?download=true>Download</a>
Then on the code behind use this:
var download = Request.QueryString["download"];
if(download == "true") {
/* place the code here */
}
Now it would only execute if the condition is true, otherwise it would skip that part.