admin管理员组文章数量:1290964
I wish to create a text file locally on my system using javascript/jquery.
I am trying this code, but not working on my system.
Machine : Ubuntu 10.4 Chrome : 14.0.835.126
window.webkitRequestFileSystem(window.TEMPORARY, 1024*1024, function(fs) {
fs.root.getFile('~/Desktop/test.txt', {create: true}, function(fileEntry) {
alert(fileEntry.fullPath); //getting filepath
}, function() {});
}, function() {});
I wish to create a text file locally on my system using javascript/jquery.
I am trying this code, but not working on my system.
Machine : Ubuntu 10.4 Chrome : 14.0.835.126
window.webkitRequestFileSystem(window.TEMPORARY, 1024*1024, function(fs) {
fs.root.getFile('~/Desktop/test.txt', {create: true}, function(fileEntry) {
alert(fileEntry.fullPath); //getting filepath
}, function() {});
}, function() {});
Share
Improve this question
edited Oct 7, 2011 at 9:09
Saurabh Saxena
asked Oct 7, 2011 at 8:58
Saurabh SaxenaSaurabh Saxena
3,21511 gold badges35 silver badges46 bronze badges
9
- Proof me wrong, but i do not think, that this is possible. You can set a cookie for sure. – madc Commented Oct 7, 2011 at 9:06
- 2 What do you mean by "not working"? Are you seeing errors? Did you write this code from scratch or copy it from a certain tutorial or reference site, and what is the URL? We need more information. – Jordan Running Commented Oct 7, 2011 at 9:17
- ChecK This Fiddle This works only for .bin files. – Saurabh Saxena Commented Oct 7, 2011 at 9:20
- I am not getting any errors. This code is working fine on jsFiddle – Saurabh Saxena Commented Oct 7, 2011 at 9:21
- If the code is working fine then what is your question? – nnnnnn Commented Oct 7, 2011 at 10:39
1 Answer
Reset to default 6This is a little tricky but working
chrome.browserAction.onClicked.addListener(createFile);
createFile();
function createFile()
{
chrome.tabs.getSelected(null, function(tab) {
window.webkitRequestFileSystem(window.TEMPORARY, 1024*1024, function(fs) {
fs.root.getFile('test', {create: true}, function(fileEntry) {
fileEntry.createWriter(function(fileWriter) {
var builder = new WebKitBlobBuilder();
builder.append("Saurabh");
builder.append("\n");
builder.append("Saxena");
var blob = builder.getBlob('text/plain');
fileWriter.onwriteend = function() {
chrome.tabs.create({"url":fileEntry.toURL(),"selected":true},function(tab){});
};
fileWriter.write(blob);
}, errorHandler);
}, errorHandler);
}, errorHandler);
});
}
function errorHandler(e) {
var msg = '';
switch (e.code) {
case FileError.QUOTA_EXCEEDED_ERR:
msg = 'QUOTA_EXCEEDED_ERR';
break;
case FileError.NOT_FOUND_ERR:
msg = 'NOT_FOUND_ERR';
break;
case FileError.SECURITY_ERR:
msg = 'SECURITY_ERR';
break;
case FileError.INVALID_MODIFICATION_ERR:
msg = 'INVALID_MODIFICATION_ERR';
break;
case FileError.INVALID_STATE_ERR:
msg = 'INVALID_STATE_ERR';
break;
default:
msg = 'Unknown Error';
break;
};
Console.Log('Error: ' + msg);
}
Because of the Security Exceptions, i cannot create/modify a file on Local System. But in this code, I am actually creating a file in a directory which is allocated for Google Chrome Temporary Files and then downloading that file into my Downloads Folder.
This is the code of the popup page of a Chrome Extension.
:)
本文标签: htmlHow to Create a Text File Locally at client side using JavaScriptJQueryStack Overflow
版权声明:本文标题:html - How to Create a Text File Locally at client side using JavaScriptJQuery - Stack Overflow 内容由网友自发贡献,该文观点仅代表作者本人, 转载请联系作者并注明出处:http://www.betaflare.com/web/1741506073a2382328.html, 本站仅提供信息存储空间服务,不拥有所有权,不承担相关法律责任。如发现本站有涉嫌抄袭侵权/违法违规的内容,一经查实,本站将立刻删除。
发表评论