我目前正在为Google Chrome创建一个扩展程序,可以保存所有图像或指向硬盘上图像的链接.
问题是我不知道如何使用JS或Google Chrome Extension API将文件保存在磁盘上.
你有个主意吗?
在做webkitRequestFileSystem的window.PERSISTENT选择谷歌浏览器,在那里我的文件系统做文件获取写的?我想在那里删除文件并让Chrome与他们互动,同时我正在构建和调试这个应用程序.
一点背景
我已经在Chrome扩展程序上工作了几天,每天多次截取给定网页的屏幕截图.我用它作为指导,事情按预期工作.
但是,有一个小的要求扩展无法满足.用户必须有权访问保存图像(屏幕截图)的文件夹,但Chrome扩展程序无权访问文件系统.另一方面,Chrome应用程序可以.因此,经过多次回顾,我得出结论,我必须创建Chrome扩展程序和Chrome应用程序.我们的想法是扩展会创建一个屏幕截图,然后将该blob发送到应用程序,然后将其作为图像保存到用户指定的位置.而这正是我正在做的事情 - 我正在扩展端创建一个screentshot的blob然后将其发送到应用程序,在那里要求用户选择保存图像的位置.
问题
直到保存部分,一切都按预期工作.blob在扩展程序上创建,发送到应用程序,由应用程序接收,用户被问到保存位置,图像被保存....这就是事情崩溃的地方.生成的图像无法使用.当我尝试打开它时,我收到一条消息"无法确定类型".以下是我正在使用的代码:
首先在EXTENSION方面,我创建一个blob并发送它,像这样:
chrome.runtime.sendMessage(
APP_ID, /* I got this from the app */
{myMessage: blob}, /* Blob created previously; it's correct */
function(response) {
appendLog("response: "+JSON.stringify(response));
}
);
Run Code Online (Sandbox Code Playgroud)然后,在APP方面,我收到blob并试图像这样保存它:
// listen for external messages
chrome.runtime.onMessageExternal.addListener(
function(request, sender, sendResponse) {
if (sender.id in blacklistedIds) {
sendResponse({"result":"sorry, could not process your message"});
return; // don't allow this extension access
} else if (request.incomingBlob) {
appendLog("from "+sender.id+": " + request.incomingBlob); …Run Code Online (Sandbox Code Playgroud)javascript google-chrome google-chrome-extension google-chrome-app
我正在尝试Firefox,IE 9,Chrome和Opera下面的代码,但是onInitFs(fs)函数没有被调用.如果我在window.requestFileSystem中添加'()'到onInitFs(window.PERSISTENT,1024*1024,onInitFs,errorHandler)该函数被调用但fs为null?有谁知道如何解决这个问题?我试试Windows 7.我非常感谢你的帮助.
<!DOCTYPE HTML>
`<html>
<head>
<script>
function errorHandler(e){
alert("errorrrr");
}
function onInitFs(fs){
alert("onInitFs");
}
function readClick(){
if (window.webkitRequestFileSystem) {
window.webkitRequestFileSystem(window.PERSISTENT, 1024*1024,onInitFs,errorHandler);
}
else {
window.requestFileSystem(window.PERSISTENT, 1024*1024, onInitFs, errorHandler);
}
alert("read finishsssss");
}
</script>
</head>
<body>
<input type="button" value="Read dir" onclick="readClick()">
<ul id="filelist"></ul>
</body>
</html>
Run Code Online (Sandbox Code Playgroud) 我想使用html5和javascript或jquery从chrome(所有版本)写入任何文件的txt.
我尝试过FileSystem API,我试过的代码是:
function onFs(fs) {
console.log('test');
fs.root.getFile('log.txt', {create: true, exclusive: true},
function(fileEntry) {
// fileEntry.isFile === true
// fileEntry.name == 'log.txt'
// fileEntry.fullPath == '/log.txt'
fileEntry.getMetaData(function(md) {
console.log(md.modificationTime.toDateString());
}, onError);
},
onError
);
}
window.webkitRequestFileSystem(TEMPORARY, 1024*1024 /*1MB*/, onFs);
但不行.
有没有办法写入文件?