Welcome to OStack Knowledge Sharing Community for programmer and developer-Open, Learning and Share
Welcome To Ask or Share your Answers For Others

Categories

0 votes
771 views
in Technique[技术] by (71.8m points)

File Write Operation in javascript

I need to write into a file in javascript.I tried below Code.I got error like"FileWriter is not defined".please help me in this.

<html>
<head>

</head>
<body>
 <input type = "button" value = "write" onclick="WriteFile()">
<script>

function WriteFile()
{
var fileWriter = new FileWriter("C:UsersananthiDesktop
eadme.txt");
fileWriter.open() ; 
fileWriter.writeLine("Another line") ; 
fileWriter.close() ;

}


</script>
</body>
</html>
See Question&Answers more detail:os

与恶龙缠斗过久,自身亦成为恶龙;凝视深渊过久,深渊将回以凝视…
Welcome To Ask or Share your Answers For Others

1 Answer

0 votes
by (71.8m points)

The File Writer API is defunct and never saw significant browser support.

You cannot write files from browser-based JavaScript. What you do instead is provide the user with a link that they can download, like this:

var filename = "readme.txt";
var text = "Text of the file goes here.";
var blob = new Blob([text], {type:'text/plain'});
var link = document.createElement("a");
link.download = filename;
link.innerHTML = "Download File";
link.href = window.URL.createObjectURL(blob);
document.body.appendChild(link);

That works on browsers that support the File API (which modern ones do, but not IE9 or earlier).


与恶龙缠斗过久,自身亦成为恶龙;凝视深渊过久,深渊将回以凝视…
Welcome to OStack Knowledge Sharing Community for programmer and developer-Open, Learning and Share
Click Here to Ask a Question

...