? How to create javascript File object with hard-coded file name?

? How to create javascript File object with hard-coded file name?
0

#1

I want to do something like the following, but cannot seem to create the file object correctly. The file must be a local file with hard-coded file name.


var elem = document.getElementById(....); // text will display here
var file = new File(... "myLocalFile.xyz",????);// **This is what I don't know**
var reader = new FileReader();
reader.onload=function(){
    elem.innerText = reader.result; // display file contents
}
reader.readAsText(file); //asynchronous

This is for my own internal use.


#2

If you’re on Windows, right click your file > select properties > copy the Location value.

Here’s a screenshot:

So your New File would look something like this based on my example:
var file = New File("C:\Users\<insert-your-local-computer-name>\Desktop\New Text Document.txt");

I’m not sure if the same thing applies to MACs.


#3

I cleaned up your code.
You need to use triple backticks to post code to the forum.
See this post for details.


#4

Thank you, ver2point10, but the problem is not the file name. It’s the arguments to the File constructor. Just using new File(filename) does not work.


#5

Thanks, kevcomedia. I’ve used the backslashes for html code, but didn’t think I needed them for javascript. I use a plain text editor, so the look of my posting was what I see when working. I will use backslashes next time.


#6

Sorry, I didn’t understand what you were really trying to do. I found this link on stackoverflow new File. Scroll down to the second answer.

var file = new File([""], "filename");