I need to browse to a folder to get the directory name by clicking a button, is there any way of getting this? I was thinking I could achieve this by using <input type="file" />
For security reasons you cant. You can get just the file name, the rest is handled by the browser.
Answered here https://stackoverflow.com/a/15201258/1248388
More documentation on the File API https://developer.mozilla.org/en/docs/Using_files_from_web_applications
Related
So, I have trying to save uploaded files in a specific folder. Say, I have an html file as shown:
<form>
<input type="file" id="image_file" />
<input type="file" id="text_file" />
<input type="submit" />
</form>
Now, I want to write a js onsubmit function for the above form such that it saves the given file in a unique folder inside a base folder.
I had been researching various ways of doing this, and the only solution I found is using a server by making ajax calls, which I dont want to do.
Is there any native way in javascript that allows me to do this?
You can not store upload files to the server only using client-side Javascript. You need any backend language/technology. Javascript is the client-side language which runs only in the client's browser, so you need to send the selected file to the server and then save it on the server using any backend language (for example, php or 'node.js`)
Check out these links:
https://www.w3schools.com/php/php_file_upload.asp
https://www.w3schools.com/nodejs/nodejs_uploadfiles.asp
For a custom WordPress plugin, I'm trying to allow my users to select a folder and use it as the file download path.
Example: If the user selects the folder "C://Users/Kahiego/Desktop/img", may it be empty or not, when they'll try to download files from the plugin, everything will get downloaded in there.
So far all I can achieve right now is allowing to select a folder:
<form action="" method="post" enctype="multipart/form-data">
<input type="file" id="ctrl" webkitdirectory directory multiple/>
<input type="submit" value="Upload Image">
</form>
(but it will only count as a multiple file select). I checked on various posts already but none can tell me how to achieve this (with some saying they don't see how this could be useful).
Any idea?
Thanks in advance.
Edit:
What I mean by this: I want the admin to be able to pick a folder. I just want to retrieve the path, and use it somewhere else; not necessarily right afterward.
After retrieving the uploaded file, I currently use:
move_uploaded_file($_FILES["file"]["tmp_name"], "../../../img/" . $_FILES["file"]["name"]);
to store the files. I just wanted to allow to store the path in a variable through an input and use it afterwards in this case.
I'm not talking about browsing user's folders, but allowing the server to pick the location where files are downloaded, on the server itself, without writing the path, just by letting the admin selecting it by browsing files.
The flagged duplicated post does not answer or not totally answer what I'm looking for, since it's either selecting a folder - with every files in there - or not allowing to select a folder which is empty. Would be perfect to reproduce the functionality of those "select a directory" functions when you first install a software.
I hope I clarified enough, I'm really bad at explaining things.
I am learning javascript. Can I use javascript to change the browse file path when input type is file?
<input type="file" id="file_input" name="file_name"/>
When I click the "browse file..." button, the window is my assign path.
I do not hope the user to find the file location from the window. I am sorry my poor English
I don't think this is possible. See here for a more complete explanation, but basically it's a security issue. Code from your web page should not be allowed to know anything about the file structure on a client machine.
I have the following html on a form:
<input type="file" name="uploadField" />
When users click on the browse button they can select any file but I want to rename the file using the value of another field on the form
I have a submit button. Can I do it here?
Can I do it here?
No, for security reasons you have no control of this on the client. You can rename the file on the server when it gets uploaded.
I know the question is old but if anyone is still struggling with similar problem, please try to use append() or set() method of the FormData.
for security reasons you cannot do such thing i.e. manipulate multipart file or file to be uploaded do that on server
I'm a beginner! I need to read data inside txt files in a local folder offline. I know the folder path, but I don't know the name of every single file.
C:\mypath\first.txt
C:\mypath\second.txt
C:\mypath\third.txt
...
To read a sigle file now I use:
$.ajax({url:"C:\mypath\first.txt",
success:function(result){
//...code for storing the data in a variable...
}
});
How can i read multiple file at once without know their name? something like:
$.ajax({url:"C:\mypath\*.txt",
success:function(result){
//...code for storing the data in a variable...
}
});
Thank you for any help!
You can use a file picker control (ie, <input type="file" multiple />) in a supported browser and have the user select the set of files to iterate. User input is the only way to get the list of files - you can't just go mucking about in a user's file system over the internet. All you can learn about the user's system is what the user tells you (eg, through <input type="file" multiple />).
And even then, you won't be able to read the file with a simple Ajax request. Same origin policies apply to local files. It may work if you test it on your own machine, but as soon as it hits the web, it will fail.
The only way to look through a client file system without user interaction is by using a Scripting.FileSystemObject ActiveXControl on windows in a non-internet based HTML Application (.hta file). So, since you don't want to use ActiveXControls, user input is your only option.
Edit: If you are creating a FireFox add-on, you can access the file system. See the documentation at mozilla.org for details.