Puppeteer: how to rename a file before downloading? - puppeteer

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?

Related

ReactJS link to local HTML file from different folder/project

I'm using ReactJS to build a site, and I want to create a link (a href="relativepath") to a local HTML file so that when the user clicks on the link, it'll open up the html page. The local file is in a different folder X outside of the project, and I don't want to upload it into my src folder because the html file depends on a lot of other files in X. Is there a good way to do so?
I also want to upload a different local HTML file that is already within the src folder of my React App. I currently have something like this:
import htmlFile from "../links/htmlFile.html"; export default function Something(props) { return (<a href={htmlFile}></a>)}
and it says in my terminal that
You may need an appropriate loader to handle this file type, currently no loaders are configured to process this file. See https://webpack.js.org/concepts#loaders
> <html>| | <head> >
I already tried adding in webpack + an htmlLoader, but I think I followed the steps incorrectly as I wasn't able to get it to work. I uninstalled those packages, so I'm now back to square one.
Thank you so much!
Just linking to or importing from a local file in some other location won't work unless those local files are also deployed to the server in the same location relative to the app (and the web server has access to that location).
So you'll need to copy the file and its linked dependencies in a folder that will be deployed along with your react build, but not where it'll get treated as part of the react codebase so webpack will try to compile it (so not in src either).
If you used create-react-app to set up your application, for example, this would be the public folder; other webpack setups may use different names but the general concept is the same.

chrome.filesystem: Rename a file and save it on the client's disk

How could I do with chrome.filesystem to rename a file and save it. For example, if my file is named myfile.txt I would rename the myfile.html and save it without using the saveAs function. And if that's not possible, do I have a solution.
The problem is that I have to save the file on the client's disk. So for me to use the filesystem functions can not be a solution, I have not seen that chrome.filesystem API that allows.
Thank you in advance! I'm a little discouraged. I also watched the browserify aside to work around the problem, but I have not found how to do it.
To rename a file you must have the ability to create a new file on the user's filesystem. You can get this permission by asking the user to open the whole directory in which the project exists. Then you can create any file you want within that directory by calling getFile with { create: true } on the resulting DirectoryEntry.
Edit: See this example for duplicating files selected by the user. Instead of using fs.root as is done here you can use the result of chrome.fileSystem.chooseEntry as the DirectoryEntry in which the file is saved.

Selenium ide to locate a file and store its path info from any computer

currently in my test scripts for automated file upload to browser, the paths are already defined in the value column
command type
target //input[#type='file']
value /Users/.../.../.../filename.extension
in such cases, this script is unable to run on other computers because the path would be different.
my question will be is
is there a way to locate the file in a general folder (for example file is downloaded and in the "download" folder), by using selenium ide can we get the path of the file (/Users/.../downloads/filename.extension)
store the path of the file with its extension into a notepad which i will be using it for multiple test of file uploads later on.
right now if my colleague needs to run the script from his computer, he have to manually change the value to his path.
You could use a suite file that contains a "setup" file to only change the file name in 1 place and the variable is shared across tests in the suite. You could also select an agreed up on place to store the files: c:\test_info\image.jpg.
Or you can make the file available by URL & not local, Unfortunately javascript prevents that for security: How to get the current file path in javascript
Unfortunately I can't think of any other good way unless you all have the same path in a home directory and could do something like ~/test_dir/photo.jpg

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.

How to get a directory path from the client using html form

I am developing some web app to be used on my local pc only.
The question is that I want to be able to browse to some directory or file and get its path. So is this possible using some file/directory path picker (not the whole dir, just its path).
I don't want to manually type the path in the text input, rather to have some visualized way to navigate to the dir.
Thanks
Previous Removed
EDITED:
$("#someFileUpload").change(InputChanged);
function InputChanged(e){
var fileList = e.target.files;
//You now have a list of the files uploaded, from here you can work with the objects..
}