Sunday, May 12, 2024
 Popular · Latest · Hot · Upcoming
17
rated 0 times [  24] [ 7]  / answers: 1 / hits: 20168  / 8 Years ago, fri, february 5, 2016, 12:00:00

I'm using a 3rd party library which wants to load in a file via the HTML file system, as in:



<input id=fileupload type=file onchange=LoadAndDisplayFile(this.files[0])>


This works great, except I'd like to pass in a url to a file stored on the serverr rather than have the user upload a file.



I tried using:



var myFile = new File (path/to/file);


with the hope that I'd then be able to pass myFile into LoadAndDisplayFile()
but I get the following error:



Uncaught TypeError: Failed to construct 'File': 2 arguments required, but only 1 present.



I'm sure this is a noob question, but what am I missing here?


More From » javascript

 Answers
29

You cannot create a File object only giving an URL to it.



The right method is to get the file through a Http request and read it, with something like this:



var blob = null
var xhr = new XMLHttpRequest()
xhr.open(GET, path/to/file)
xhr.responseType = blob
xhr.onload = function()
{
blob = xhr.response
LoadAndDisplayFile(blob)
}
xhr.send()

[#63435] Wednesday, February 3, 2016, 8 Years  [reply] [flag answer]
Only authorized users can answer the question. Please sign in first, or register a free account.
pranavrorys

Total Points: 466
Total Questions: 87
Total Answers: 115

Location: Barbados
Member since Sun, Nov 27, 2022
1 Year ago
;