html or javascript code to create a text file in hard disk

前端 未结 2 1023
星月不相逢
星月不相逢 2021-01-29 01:59

Please someone give me a code to create a text file in hard drive

Result should be a html file, when double click html file it need to create a text fil

2条回答
  •  时光说笑
    2021-01-29 02:32

    Something tells me that you haven't enough experience to know that a web page cannot create a file in the user's space in the position you want.

    Anyway, there is a way, but you can only create a file in a sandbox, i.e. a reserved and protected space assigned by the browser.

    This is possible only in the most recent browsers like Chrome... and nothing else, for now.

    First, you'll have to ask for some quota:

    storageInfo.requestQuota(PERSISTENT, bytes, function(quota) {
       requestFileSystem(PERSISTENT, quota, gotQuota, errorHandler);
    }, function(e) {
       alert("Couldn't request quota:" + e);
    });
    

    You'll have to define two callback functions: one for success (gotQuota) and one for failure (errorHandler).

    function gotQuota(fs) {
        // Creates a file
        fs.root.getFile('file.txt', {create: true}, function(fileEntry) {
           fileEntry.createWriter(function(fw) {
               fileWriter.onwriteend = function(e) {
                   console.log("Write successful.");
               };
               fileWriter.onerror = function(e) {
                   console.log("Write failed: " + e);
               };
               fw.write(new Blob(["This is the content"], {type: "text/plain"});
           });
        }, errorHandler);
    }
    

    Man, it's complicated... Keep in mind that some of these function are vendor prefixed (e.g. webkitStorageInfo).

    Reference.

提交回复
热议问题