get file object using full path - javascript

is there a way to get a file object using the directory full path of the file itself? this piece of code wont work:
var file1 = new File("D:\\path\\to\\file\\file.txt");
I need to get the file object since I have a function that has a file object as its parameter.
bryan

That would be very tragic if browsers could suddenly start accessing users' file systems without their permission.
I would suggest using <input type="file">. This way the user chooses which file they will allow the browser to access.

I would suggest you to use: <input type="file" id="fileUpload"> and get the file name using
$('input[type=file]').change(function () {
console.log(this.files[0])
});

Related

How to read hardcoded image file using javascript / jquery

Is it possible to use hardcoded filename?
I want to read File of an image.
I have hardcoded filename like, '/Users/Desktop/1.jpg' and i want to use this
as a file.
I want to change this '/Users/Desktop/1.jpg' type as file.
var imageURL = '/Users/Desktop/1.jpg';
new File(imageURL);
alert(typeof(imageURL)); // I want this as a File
The simplest approach would be to click or drag and drop the file at an <input type="file"> element.
<input type="file" accept="image/*">
<script>
const input = document.querySelector("input");
input.onchange = function(event) {
const file = event.target.files[0];
console.log(file);
}
</script>
If the file is served with CORS headers you can request the file using Image constructor and pass the <img> reference to canvas.toBlob(), then pass Blob reference to File constructor, optionally set the name of the File object.
Similarly CORS headers are necessary when using fetch() and Response.blob() to get Blob of image.
Note if using Chrome or Chromium browser to request files at local filesystem you can launch Chrome or Chromium browsers with --allow-file-access-from-files flag to access local filesystem at JavaScript and HTML, see Read local XML with JS.

How to define a local file as a DOM object?

I want to parse data in my computer using JavaScript. I use papa parse .
In PapaParse documentation it has been stated local files can be parsed by following code ;
Papa.parse(file, config)
In documentation they say file is a File object obtained from the DOM. How can I define a local file as an DOM object ?
I doubt that this is possible as it would be a huge security problem. Imagine someone could just read files from your computer when you load their javascript through your browser. You'll have to go with a file picker.
<script src="https://code.jquery.com/jquery-1.11.3.js"></script>
<input type="file" id="csv-files" multiple>
<script>
var handleFileSelect(evt) {
var file = evt.target.files[0];
// do stuff with that file e.g. Papa.parse(file)
}
$(document).ready(function() {
$("#csv-files").change(handleFileSelect);
});
</script>
the above code uses jQuery to load the files when the file picker was used.
see joyofdata.de/parsing-local-csv-file for reference. Concise explanation.

javascript read file object unknown id

Is it possible to use javascript File API to read a file object of unknown id?
I use a custom file upload form in which the code doesn't have an input element. It uses plupload.js API and I can't determine how to access to the contents of file object.
For example if the code had something like the line 1, I could access the contents of file object using the code of line 2
input type="file" id="data"
var finput = document.getElementById("data"); var f = finput.files[0];
I tried something like document.files[0] but it doesn't work. Do you know something similar?
Instead of document.files[0], try document.querySelectorAll('input[type="file"]')[0].
function demo(){
var inpts=document.querySelectorAll('input[type="file"]');
alert(inpts[0].className);
}
<input type="file" class="file_input_a"><br>
<input type="file" class="file_input_b"><br>
<input type="file" class="file_input_b"><br>
<button onclick="demo()">Demo</button>
The above example echo's the classname of file-input 0 (to simply show we have a reference to it).
From here on you want to grab (for example) first file (from the first found input-element):
var f = inpts[0].files[0];
PS: there is a lot of good related info in my answer linked here.

Javascript: variable file from string

Hopefully this is a pretty simple question for those who know javascript but I couldn't figure it out. Basically I have a save file dialog box created with this:
<div>
<input type="file" id="file" onchange="loadFile(this)">
</div>
It is linked to the "loadfile" function shown below:
function loadFile(input) {
var file = input.files[0]
var url = file.urn || file.name;
ID3.loadTags(url, function() {showTags(url);}, {tags: ["title","artist","album","picture"],dataReader: FileAPIReader(file)});
}
I am trying to replace the line
var file = input.files[0]
to be a file from a string (specified path) instead of relying on the file browser dialog. I have tried something like this:
var file = files("song.mp3")
but the resulting function will not work. I am guessing that my variable file isn't of the right type. How do I get it to be the same type as the selected file from the file dialog box?
Thanks in advance!
PS:
I am trying to link my script to a path on the server not the client.
If you want to access data from a website, then use the XMLHttpRequest object instead of the Files API.

file path from input tags in javascript

Can I pick the file path from an input tag in javascript? For example:
<input id="file" type="file" onchange="getpath()" />
This can be done in some older browsers, but in correct implementations, the Javascript security model will prevent you from reading the path.
You can’t get at the full path to the file (which would reveal information about the structure of files on the visitor’s computer). Browsers instead generate a fake path that is exposed as the input’s value property. It looks like this (for a file named "file.ext"):
C:\fakepath\file.ext
You could get just the filename by splitting up the fake path like this:
input.onchange = function(){
var pathComponents = this.value.split('\\'),
fileName = pathComponents[pathComponents.length - 1];
alert(fileName);
};

Categories