JavaScript-新File()无法正常工作 [英] JavaScript - New File() not working
问题描述
我正在尝试通过以下代码使用JavaScript将一些字符串写入文件中
I am trying to write some strings to a file using JavaScript through the following code
var txtfile ="../wp-content/plugins/MedicAdvisor/test.txt";
var file = new File("hello",txtfile);
//file = fopen("", 3);// opens the file for writing
file.open("w");
var currentrow = 0;
var nextrow = 0;
var type = " ";
var noofrows = 0;
var noofcells = 0;
var contentarray;
var row = document.getElementsByTagName('tr');
//get all elements having input tag
var inp = document.getElementsByTagName('input');
// traverse through all input tags
for (var i=2; i<inp.length; i++){
// see if it is a heckbox
if(inp[i].type == "checkbox"){
// see if it is checked
if(inp[i].checked == true){
//index of current row
currentrow = inp[i].parentNode.parentNode.rowIndex;
//event type
type = inp[i].parentNode.parentNode.cells[6].innerHTML.trim();
if (type == "cycling_road_race"){
noofrows = 6;
for(var j=0; j<noofrows; j++){
noofcells = row[currentrow + j + 1].cells.length;
for (var k=1; k<noofcells; k++){
//alert (row[currentrow + j + 1].cells[k].innerHTML.replace('<br>' , ' '));
contentarray.push(row[currentrow + j + 1].cells[k].innerHTML.replace('<br>' , ' '));
file.writeln(row[currentrow + j + 1].cells[k].innerHTML.replace('<br>' , ' '));
}
}
}
else if (type == "cycling_criterium_or_circuit_race"){
noofrows = 6;
}else if (type == "cycling_cyclocross"){
noofrows = 6;
}else if (type == "running_race"){
noofrows = 6;
}else if (type == "rugby_football_hockey"){
noofrows = 6;
}else if (type == "music_festival"){
noofrows = 6;
}else if (type == "manual_selection"){
noofrows = 5;
}
}
}
}
但是当我尝试执行此代码时出现以下错误
but I am getting following error when I try to execute this code
无法构造文件":第一个参数既不是数组,也不是它具有索引属性
Failed to construct 'File': The 1st argument is neither an array, nor does it have indexed properties
请帮助我解决此问题
推荐答案
如错误消息所示, File
构造函数期望将数组作为第一个参数.同样,第二个参数应仅是文件名和扩展名.您还可以将 type
设置为有效的 MIME
类型,将 lastModified
作为对象的属性,设置为 File
构造函数的第三个参数
As the error message indicated, File
constructor expects an array as first parameter. Also the second parameter should only be the file name and extension. You can also set type
as a valid MIME
type and lastModified
as properties of object at third parameter to File
constructor.
var txtfile = "test.txt";
var file = new File(["hello"], txtfile
, {type:"text/plain", lastModified: new Date().getTime()});
File.prototype
没有 .open
方法.您可以使用 File.prototype.slice()
创建新的 File
对象,并将新数据连接到先前创建的 File
对象./p>
File.prototype
does not have an .open
method. You can use File.prototype.slice()
to create a new File
object and concatenate data new data to the previously created File
object.
file = new File([file.slice(0, file.length), /* add content here */], file.name);
将 File
对象保存到服务器需要将 File
对象发布到服务器以读取文件数据的内容.
Saving a File
object to server requires posting the File
object to server to read the contents of the file data.
var request = new XMLHttpRequest();
request.open("POST", "/path/to/server");
request.send(file);
使用 php://input
$input = fopen("php://input", "rb");
请参见尝试使用输入类型文本传递超过524288字节的ToDataURL
这篇关于JavaScript-新File()无法正常工作的文章就介绍到这了,希望我们推荐的答案对大家有所帮助,也希望大家多多支持IT屋!