Logo Questions Linux Laravel Mysql Ubuntu Git Menu
 

How to convert string to File object in javascript ?

I am trying to convert string to File object in javascript but I get an error.

my code:

 var contents = fs.readFileSync('./dmv_file_reader.txt').toString()

 var  readerfile1 = new File([""], contents);

(i have to use contents as file and not as string)

and my output is :

ReferenceError: File is not defined
    at d:\Workspace\DMV\dist\win-ia32-unpacked\resources\app.asar\main.js:67:32
    at process._tickCallback (internal/process/next_tick.js:103:7)

any solution some1?

like image 894
Tom Cohen Avatar asked Nov 29 '16 15:11

Tom Cohen


1 Answers

First you have to create blob from Javascript object and that blob object can be passed to File() constructor to create a File Object.Hope this helps.

 var contents = fs.readFileSync('./dmv_file_reader.txt').toString()
 var blob = new Blob([contents], { type: 'text/plain' });
 var file = new File([blob], "foo.txt", {type: "text/plain"});
like image 149
sachin Avatar answered Sep 29 '22 00:09

sachin