问题
I have an html file on my desktop that takes some input. How would I go about writing that input into a file onto my computer? Would I have to use another language to do it (i.e python or javascript?) and how would I go about doing this? On a related note, is there any way I can have javascript start an application from within an html file (the goal is to write to a file on my computer?
回答1:
Browsers have lots of security that prevent this level of control over your computer. This is a good thing. You dont want random websites to be able to do this stuff on anyone's computer that visits them.
They way to do this would be to write a web application that your browser could access. The browser can submit data to this application running on your own computer and your application could manipulate the file system or do lots of other things.
So no, a browser can't do these things. And yes, you would have to use "another language" to create something which runs outside the browser itself. You can use javascript (see node.js) or python to do this, as well nearly any other programming language that exists to create such a thing. Which to choose is up to you.
回答2:
You would have to use another language. You should see Save file Javascript with file name (this requires you creating it on your website and having the user download it) and Python Save to file (for implementing Python in HTML see here). Or in Javascript you could use ActiveX to save a file by doing*:
ActiveX
function WriteToFile()
{
var fso = new ActiveXObject("Scripting.FileSystemObject");
var s = fso.CreateTextFile("C:\\Test.txt", true);
s.WriteLine('Hello');
s.Close();
}
Python
myFile = open('Failed.py', 'w')
myFile.write('whatever')
myFile.close()
Javascript (non ActiveX)
uriContent = "data:application/octet-stream;filename=filename.txt," +
encodeURIComponent(codeMirror.getValue());
newWindow=window.open(uriContent, 'filename.txt');
**Note: I really do not recommend using ActiveX
, see ActiveX and Javascript do not mix for more details*
回答3:
Right now writing to a local file with Javascript can be done using the FileWriter
API that is currently only supported by Chrome and Opera:
http://caniuse.com/#search=filewriter
Here is some sample code from HTML5Rocks.com:
function onInitFs(fs) {
fs.root.getFile('log.txt', {create: true}, function(fileEntry) {
// Create a FileWriter object for our FileEntry (log.txt).
fileEntry.createWriter(function(fileWriter) {
fileWriter.onwriteend = function(e) {
console.log('Write completed.');
};
fileWriter.onerror = function(e) {
console.log('Write failed: ' + e.toString());
};
// Create a new Blob and write it to log.txt.
var blob = new Blob(['Lorem Ipsum'], {type: 'text/plain'});
fileWriter.write(blob);
}, errorHandler);
}, errorHandler);
}
window.requestFileSystem(window.TEMPORARY, 1024*1024, onInitFs, errorHandler);
http://www.html5rocks.com/en/tutorials/file/filesystem/
回答4:
Your question is a little hard to answer. If by "it takes some input" you mean there's a form
element involved then you can process that via a language like PHP.
If you're just looking to edit the code and get started with learning HTML, then Notepad/TextEdit and a modern web browser are your new best friends.
回答5:
Short answer:
you can't. The web browser security and sandbox'ing prevents this.
Longer answer:
setup a small LAMP stack, Ruby on rails(local host server), Python/Django(local host server) to host your HTML/web form. The local daemon can handle appending data to a file, as you enter it into a form.
With HTML5, you get special hooks that may allow you to write to the local filesystem, but those may be browser specific and may break from time to time.
来源:https://stackoverflow.com/questions/12468335/how-to-create-a-file-and-append-data-from-html-page