如何在javascript中将字符串转换为File对象?

问题描述 投票:0回答:1

我正在尝试在javascript中将字符串转换为File对象,但出现错误。

我的代码:

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

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

(我必须将内容用作文件而不是字符串)

我的输出是:

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)

任何解决方案some1?

javascript file type-conversion readfile
1个回答
0
投票

首先,您必须从Javascript对象创建blob,然后将该blob对象传递给File()构造函数以创建File对象。希望这会有所帮助。

 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"});
© www.soinside.com 2019 - 2024. All rights reserved.