Cách ghi file bằng javascript trong Chrome

Bạn nào có kiến thức phần này cho mình hỏi...Mình muốn ghi một file text bằng javascript, nhưng mình không biết code như thế nào mong các bạn giúp giùm. Mình có đoạn code như thế này:
<html>
<head>
<script type="text/javascript">
function WriteFile()
{
var fso = new ActiveXObject("Scripting.FileSystemObject");
var fh = fso.CreateTextFile("E:\\Test.txt", true);
fh.WriteLine("Some text goes here...");
fh.WriteLine("new ...");
fh.Close();
}
</script>
</head>
<body onLoad="WriteFile()">
</body>
</html>

Nhưng nó chỉ hoạt động trong IE, bạn nào có cách khác để có thể chạy trong Chrome được không mong các bạn giúp đỡ!!
Rất cảm ơn
 
Ðề: Cách ghi file bằng javascript trong Chrome

oh. bạn cần phải tham khảo một số công cụ trên forum khác nữa chứ nữa chứ.hay một số nguồn tại liệu đó. bạn chạy = html ah,
 
Hoàn toàn có thể :
Mã:
<html>
<body>


<table>
    <tr><td>Text to Save:</td></tr>
    <tr>
        <td colspan="3">
            <textarea id="inputTextToSave" style="width:512px;height:256px"></textarea>
        </td>
    </tr>
    <tr>
        <td>Filename to Save As:</td>
        <td><input id="inputFileNameToSaveAs"></input></td>
        <td><button onclick="saveTextAsFile()">Save Text to File</button></td>
    </tr>
    <tr>
        <td>Select a File to Load:</td>
        <td><input type="file" id="fileToLoad"></td>
        <td><button onclick="loadFileAsText()">Load Selected File</button><td>
    </tr>
</table>


<script type='text/javascript'>


function saveTextAsFile()
{
    var textToWrite = document.getElementById("inputTextToSave").value;
    var textFileAsBlob = new Blob([textToWrite], {type:'text/plain'});
    var fileNameToSaveAs = document.getElementById("inputFileNameToSaveAs").value;


    var downloadLink = document.createElement("a");
    downloadLink.download = fileNameToSaveAs;
    downloadLink.innerHTML = "Download File";
    if (window.webkitURL != null)
    {
        // Chrome allows the link to be clicked
        // without actually adding it to the DOM.
        downloadLink.href = window.webkitURL.createObjectURL(textFileAsBlob);
    }
    else
    {
        // Firefox requires the link to be added to the DOM
        // before it can be clicked.
        downloadLink.href = window.URL.createObjectURL(textFileAsBlob);
        downloadLink.onclick = destroyClickedElement;
        downloadLink.style.display = "none";
        document.body.appendChild(downloadLink);
    }


    downloadLink.click();
}


function destroyClickedElement(event)
{
    document.body.removeChild(event.target);
}


function loadFileAsText()
{
    var fileToLoad = document.getElementById("fileToLoad").files[0];


    var fileReader = new FileReader();
    fileReader.onload = function(fileLoadedEvent) 
    {
        var textFromFileLoaded = fileLoadedEvent.target.result;
        document.getElementById("inputTextToSave").value = textFromFileLoaded;
    };
    fileReader.readAsText(fileToLoad, "UTF-8");
}


</script>


</body>
</html>
Chúc bạn thành công!
 

Thống kê

Chủ đề
102,787
Bài viết
470,612
Thành viên
340,593
Thành viên mới nhất
winspire
Top