JavaScript – 新文件()不起作用
内容导读
互联网集市收集整理的这篇技术教程文章主要介绍了JavaScript – 新文件()不起作用,小编现在分享给大家,供广大互联网技能从业者学习和参考。文章包含2477字,纯文字阅读大概需要4分钟。
内容图文
![JavaScript – 新文件()不起作用](/upload/InfoBanner/zyjiaocheng/698/3fcc821db05548e8baf5532dc60cce30.jpg)
我试图通过以下代码使用JavaScript将一些字符串写入文件
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;
}
}
}
}
但是当我尝试执行此代码时,我收到以下错误
Failed to construct ‘File’: The 1st argument is neither an array, nor
does it have indexed properties
请帮我解决这个问题
解决方法:
如指示的错误消息,File构造函数需要将数组作为第一个参数.第二个参数也应该只是文件名和扩展名.您还可以将type设置为有效的MIME类型,并将lastModified设置为File构造函数的第三个参数的object属性.
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对象.
file = new File([file.slice(0, file.length), /* add content here */], file.name);
将File对象保存到服务器需要将File对象发布到服务器以读取文件数据的内容.
var request = new XMLHttpRequest();
request.open("POST", "/path/to/server");
request.send(file);
其中文件内容可以使用php:// input在php上读取
$input = fopen("php://input", "rb");
见Trying to Pass ToDataURL with over 524288 bytes Using Input Type Text
内容总结
以上是互联网集市为您收集整理的JavaScript – 新文件()不起作用全部内容,希望文章能够帮你解决JavaScript – 新文件()不起作用所遇到的程序开发问题。 如果觉得互联网集市技术教程内容还不错,欢迎将互联网集市网站推荐给程序员好友。
内容备注
版权声明:本文内容由互联网用户自发贡献,该文观点与技术仅代表作者本人。本站仅提供信息存储空间服务,不拥有所有权,不承担相关法律责任。如发现本站有涉嫌侵权/违法违规的内容, 请发送邮件至 gblab@vip.qq.com 举报,一经查实,本站将立刻删除。
内容手机端
扫描二维码推送至手机访问。