JavaScript - New File() 不起作用

标签 javascript

我正在尝试通过以下代码使用 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 构造函数的第三个参数处的对象属性.

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://inputphp 读取文件内容

$input = fopen("php://input", "rb");

参见Trying to Pass ToDataURL with over 524288 bytes Using Input Type Text

关于JavaScript - New File() 不起作用,我们在Stack Overflow上找到一个类似的问题: https://stackoverflow.com/questions/41116513/

相关文章:

javascript - setInterval 'stacking' 向上

javascript - 带有弹出框的动画

javascript - 将 Javascript 动态应用到 slick.js Slider 中的视频

javascript - Firefox Addon 中的 OnBeforeRequest URL 重定向(从 Chrome 扩展转换)

javascript - 检查URL在纯JavaScript中是否有效

javascript - 选项选择菜单,检测第一个选项是否被选中,而不是第一个选项的值

javascript - Express 使用无模式动态基本 URL 来呈现不同的页面

javascript - 如何处理 Laravel 到 Vue.js 的 bool 值

javascript - 关闭一个后重新打开模式,laravel

javascript - 为什么 document.activeElement 在 Mac 上使用 Firefox 会产生不同的结果