最新消息:雨落星辰是一个专注网站SEO优化、网站SEO诊断、搜索引擎研究、网络营销推广、网站策划运营及站长类的自媒体原创博客

JavaScript - New File() not working - Stack Overflow

programmeradmin1浏览0评论

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

Kindly help me resolve this issue

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

Kindly help me resolve this issue

Share Improve this question edited Dec 13, 2016 at 8:58 GG. 21.9k14 gold badges92 silver badges133 bronze badges asked Dec 13, 2016 at 8:23 Faizan ZahidFaizan Zahid 391 gold badge3 silver badges8 bronze badges 5
  • 1 It looks to me like you want to write the file on the webserver. This is not possible with javascript, because Javascript is only run in the viewers browser and has no direct server filesystem access. – Seb Commented Dec 13, 2016 at 8:28
  • 1 If you want to use the JavaScript File class then you are not using the constructor correctly (see implementation notes on MDN). However as @Seb pointed out, you seem to be trying to do something that is not supported on browsers – UnholySheep Commented Dec 13, 2016 at 8:31
  • In other words, what you are trying to do is only possible via backend/server-side code. Javascript is frontend. – John Evans Solachuk Commented Dec 13, 2016 at 8:34
  • What I am trying to do is .. export the contents of html table to txt file .. Kindly help me how to do this – Faizan Zahid Commented Dec 13, 2016 at 8:37
  • Are you trying to save data at a server? – guest271314 Commented Dec 13, 2016 at 8:42
Add a ment  | 

1 Answer 1

Reset to default 1

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 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);

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);

where file contents can be read at php using php://input

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

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

发布评论

评论列表(0)

  1. 暂无评论