Friday, May 10, 2024
 Popular · Latest · Hot · Upcoming
104
rated 0 times [  109] [ 5]  / answers: 1 / hits: 15255  / 5 Years ago, wed, april 10, 2019, 12:00:00

With the given file path, create a file object. new File(file_path) doesn't work. (WIN/MAC)



When tried creating a new file object using File constructor. There occurs an error.



new File(decodeURI(file_path))


when the above approach is followed File constructor err comes up.


More From » jquery

 Answers
4

File API needs a Blob here is work-arround



var GetFileBlobUsingURL = function (url, convertBlob) {
var xhr = new XMLHttpRequest();
xhr.open(GET, url);
xhr.responseType = blob;
xhr.addEventListener('load', function() {
convertBlob(xhr.response);
});
xhr.send();
};

var blobToFile = function (blob, name) {
blob.lastModifiedDate = new Date();
blob.name = name;
return blob;
};

var GetFileObjectFromURL = function(filePathOrUrl, convertBlob) {
GetFileBlobUsingURL(filePathOrUrl, function (blob) {
convertBlob(blobToFile(blob, 'testFile.jpg'));
});
};
var FileURL=test/test.jpg
GetFileObjectFromURL(FileURL, function (fileObject) {
console.log(fileObject);
});

[#52270] Saturday, April 6, 2019, 5 Years  [reply] [flag answer]
Only authorized users can answer the question. Please sign in first, or register a free account.
dequant

Total Points: 88
Total Questions: 99
Total Answers: 95

Location: Ukraine
Member since Sun, Dec 13, 2020
3 Years ago
;