6

if (window.XMLHttpRequest)
{
 xmlhttp=new XMLHttpRequest();
}
else
{
 xmlhttp=new ActiveXObject("Microsoft.XMLHTTP");
}
xmlhttp.open("GET","t1.txt",false);
xmlhttp.send();
xmlDoc=xmlhttp.responseText;
xmlhttp.open("w","t1.txt");
xmlhttp.writeln('hai');
xmlhttp.close();
console.log(xmlDoc);

I'll read t1.txt file using XMLHttpRequest(), How I'll write some more text in same file for t1.txt

Shaffiulla Khan
10.4k16 gold badges58 silver badges54 bronze badges
asked Feb 22, 2016 at 7:18
1

1 Answer 1

24

You can't. Browser-side javascript are not allowed to write to client machine without disabling a lot of security options.

Instead, you could download a new file using this code

function downloadContent(name, content) {
 var atag = document.createElement("a");
 var file = new Blob([content], {type: 'text/plain'});
 atag.href = URL.createObjectURL(file);
 atag.download = name;
 atag.click();
}
downloadContent("t1.txt","hello world");
answered Feb 22, 2016 at 7:37
Sign up to request clarification or add additional context in comments.

Comments

Your Answer

Draft saved
Draft discarded

Sign up or log in

Sign up using Google
Sign up using Email and Password

Post as a guest

Required, but never shown

Post as a guest

Required, but never shown

By clicking "Post Your Answer", you agree to our terms of service and acknowledge you have read our privacy policy.

Start asking to get answers

Find the answer to your question by asking.

Ask question

Explore related questions

See similar questions with these tags.