This question already has answers here:
How to get full path of selected file on change of <input type=‘file’> using javascript, jquery-ajax?
(14 answers)
Closed 9 years ago.
I have created <input type="file" name="files" id="fld1" webkitdirectory > in an html file.
i can get the files using
var imageFiles = document.getElementById("fld1"),
filesLength = imageFiles.files.length;
for (var i = 0; i < filesLength; i++) {
alert(imageFiles.files[i].name);
}
How can i get the full path of the directory selected in javascript . please help
I want to show all the image files present in any folder using jquery slider. i can get the name of the files present in that folder but i cannot get the full path of the folder. how can i get it.
i have done this to get the file path but it only worked in IE
<script language="JavaScript">
function GetDirectory() {
strFile = document.MyForm.MyFile.value;
intPos = strFile.lastIndexOf("\\");
strDirectory = strFile.substring(0, intPos);
alert(strFile + '\n\n' + strDirectory);
return false;
}
</script>
For obvious security reasons you cannot do that. Browsers won't allow you to access and explore the file system of the client computers using javascript. Some browsers will simply return some fake path to the file.
Check File API documentation, the File section gives details about the File object.
Quoting from the doc:
The name of the file; on getting, this must return the name of the
file as a string. There are numerous file name variations on different
systems; this is merely the name of the file, without path
information. On getting, if user agents cannot make this information
available, they must return the empty string.
Related
This question already has answers here:
Search for files inside of a folder using drive api
(2 answers)
Is there a way in google script to access files inside a folder through a shortcut to the folder by folder name?
(1 answer)
Finding subfolders within parent folder
(2 answers)
Closed 3 months ago.
This post was edited and submitted for review 3 months ago and failed to reopen the post:
Original close reason(s) were not resolved
I got a questions regarding the DriveApp.folders() function for the google Scripts. I just implemented a script which add a description to all my files, that the last change date will get updated. For this I used the DriveApp.folders() with .getfiles(). Now my Problem: this function access all folders, even the shared folders, but it should only access the Folders in My Drive? How can I handle this?
I tried already the .getOwner() function, but with this i get 'DriveUser' as Answer with some shared drive folders.
I tried also to get the access by .getFolderbyID() but there is no ID for my drive.
Here is my code:
function writeDescription() {
var folder = DriveApp.getFolders();
var count = 0;
while (folder.hasNext()){
var ordner = folder.next();
var files = ordner.getFiles();
Logger.log(ordner.getName())
//Logger.log(files)
while (files.hasNext()){
var file = files.next();
count = count +1;
file.setDescription(" ");
//Logger.log(file.getName());
}
}
Logger.log(count);
}
use https://developers.google.com/apps-script/reference/drive/folder#getOwner() and compare to your email to get only the ones you own.
This question already has answers here:
Local file access with JavaScript
(14 answers)
How to read a text file containing html without using input type file in JavaScript?
(2 answers)
Closed 3 years ago.
Lets consider that I have a text file named text.txt. So, now I made a file named showtext.html in which I have to write javascript such that it can display the text present in text.txt. I assume it would be like :
Text ( text.txt )
Show me up !
HTML ( showtext.html )
<!DOCTYPE html>
<html>
<body>
<script>
var src = "text.txt";
document.write( value.get( src ) );
</body>
</html>
Output of showtext.html
Show me up !
In, the above function the value.get() gets and then writes the value of the text.txt. So, can javascript really do such ? How can I make the value.get() ? Will it support only .txt extension or can support any custom extension ? Or this can only be done with PHP ?
"Local" means "all client, no server" :) If you don't have a server, you can't use things like fetch() (as far as I know, security restrictions prevent that). But you can read a file from the local computer on the client as long as the user selects the file themselves. You can't just read any arbitrary file from the client's computer (again, for security reasons).
If you're okay with letting the user select the file, look into the FileReader API.
HTML:
<input type='file' id='fileSelector' />
JS:
const fileInput = document.getElementById('fileSelector');
fileInput.addEventListener('change', event => {
const file = event.target.files[0];
const reader = new FileReader();
reader.addEventListener('load', readEvent => {
// In here, readEvent.target.result will have the text of the file for you to work with.
});
reader.readAsText(file);
});
I am having an issue with changing a file name.
The main issue I am having is when the user takes a picture from an iPhone. iOS names all just captured photos image.jpg. I am trying to give the user a chance to change the file name or do something programmatically, like "image (1).jpg".
I am looking for some advice on renaming files in JS or some other idea on how to handle preventing users from uploading a file with the same name.
function processSelectedFiles(fileInput) {
var files = fileInput.files;
var div = document.getElementById("fileList");
for (var i = 0; i < files.length; i++) {
var newFileName = prompt("Filename " + files[i].name,files[i].name);
if (newFileName!== null){
files[i].name=newFileName;
}
div.innerText += files[i].name;
}
}
<!-- Learn about this code on MDN: https://developer.mozilla.org/en-US/docs/Web/API/File/name -->
<input type="file" multiple onchange="processSelectedFiles(this)">
</br>
File Name:
</br>
<div id="fileList"></div>
For security reason javascript in browser doesn't have the access to the file system.
So if you try to get the full path from front end it's not possible
check here
https://www.w3schools.com/jsref/tryit.asp?filename=tryjsref_fileupload_value
You can create an api and hook it in the front end for making any file name changing operations and etc.
There are some alternative approaches provided by Firefox(mozFullPath) but it's not widely accepted at this point.
This question already has answers here:
How do i get the full path of the file in Firefox
(1 answer)
How to get the file path from HTML input form in Firefox 3
(8 answers)
Closed 8 years ago.
So far I've been able to get only the file names from the browse button by using just jQuery and HTML file form. Here is my fiddle: http://jsfiddle.net/p42Wv/2/
MY ATTEMPT
$("#file").bind("change", function() {
var elem = document.getElementById("file");
var names = [];
for (var i = 0; i < elem.files.length; ++ i) {
names.push(elem.files[i].name);
}
var names2 = names.join('\n');
$("#filename").val( names2 );
});
I get the file names as I'm supposed to BUT how do i get the Folder Names as well as the file names when I select a mixed list of files and folders in the "Browse" button?
EDIT:
NOTE: I'm looking for Folder Name and NOT the Folder Path.
I've seen website such as Google Drive that lets user's select a WHOLE folder to upload. Isn't the mere extraction of folder names such an issue? If not, how does Google Drive for instance, access the subfiles of a folder and upload?
Thanks a lot,
John
This question already has answers here:
How to upload file with Phonegap and JqueryMobile?
(2 answers)
Closed 3 years ago.
How to use html file attribute in phonegap ? so that i can browse any .txt file from my android device and upload it to the server??
I read file transfer method in phonegap documentation but according to that it is possible to upload files to the server only through url.But is it possible in normal way like :
<input type=file /><button>upload</button>
input type="file" accept="image/*" doesn't work in phone gap? i looked at this link but that says only for images .
But how to upload the text files ?
Any help??
Can anybody answer this?
You can't use input file on Phonegap. It's not supported. You need make something like this:
function uploadDatabase(callback) {
// file.entry accessible from global scope; see other tutorial
var filepath = file.entry.fullPath,
uploadOptions = new FileUploadOptions(),
transfer = new FileTransfer(),
endpoint = "http://facepath.com/payload";
uploadOptions.fileKey = "db";
uploadOptions.fileName = "database.db";
uploadOptions.mimeType = "text/plain";
transfer.upload(filepath, endpoint, transferComplete,
transferError, uploadOptions);
}
in mime type put the type of file
I wrote this answer "You can't use input file on Phonegap. It's not supported.". Check the solution on https://stackoverflow.com/a/13862151/1853864