Javascript Phonegap下载html文件并替换它(源代码)

Javascript Phonegap下载html文件并替换它(源代码),javascript,html,cordova,download,Javascript,Html,Cordova,Download,我想知道如何用下载的文件替换或添加Phonegap的一些源文件 <!DOCTYPE html> <html> <head> <title></title> <script type="text/javascript" charset="utf-8" src="cordova-2.3.0.js"></script> <script type="text/

我想知道如何用下载的文件替换或添加Phonegap的一些源文件

<!DOCTYPE html>
<html>
    <head>
        <title></title>
        <script type="text/javascript" charset="utf-8" src="cordova-2.3.0.js"></script>
        <script type="text/javascript">
               function onBodyLoad()
            {
                document.addEventListener("deviceready", onDeviceReady, false);
            }

            function downloadFile(){
                window.requestFileSystem(
                                         LocalFileSystem.PERSISTENT, 0,
                                         function onFileSystemSuccess(fileSystem) {
                                         fileSystem.root.getFile(
                                                                 "dummy.html", {create: true, exclusive: false},
                                                                 function gotFileEntry(fileEntry){
                                                                 var sPath = fileEntry.fullPath.replace("dummy.html","");
                                                                 var fileTransfer = new FileTransfer();
                                                                 fileEntry.remove();

                                                                 fileTransfer.download(
                                                                                       "https://dl.dropbox.com/u/xxxxx/index_2.html",
                                                                                       sPath + "index_aerosoft.html",
                                                                                       function(theFile) {
                                                                                       console.log("download complete: " + theFile.toURI());
                                                                                       showLink(theFile.toURI());
                                                                                       },
                                                                                       function(error) {
                                                                                       console.log("download error source " + error.source);
                                                                                       console.log("download error target " + error.target);
                                                                                       console.log("upload error code: " + error.code);
                                                                                       }
                                                                                       );
                                                                 },
                                                                 fail);
                                         }, 
                                         fail);

            }

            function showLink(url){
                alert(url);
                var divEl = document.getElementById("ready");
                var aElem = document.createElement("a");
                aElem.setAttribute("target", "_blank");
                aElem.setAttribute("href", url);
                aElem.appendChild(document.createTextNode("Ready! Click To Open."))
                divEl.appendChild(aElem);

            }


            function fail(evt) {
                console.log(evt.target.error.code);
            }

            function onDeviceReady()
            {
                downloadFile();
            }

            </script>
    </head>
    <body onload="onBodyLoad()">
            <br />
            <p>
            DOWNLOADING FILE...<br />
            <span id="ready"></span>
            </p>
            </body>
</html>

函数onBodyLoad()
{
文件。添加的监听器(“deviceready”,OnDeviceraddy,false);
}
函数下载文件(){
window.requestFileSystem(
LocalFileSystem.PERSISTENT,0,
函数onFileSystemsSuccess(文件系统){
fileSystem.root.getFile(
“dummy.html”{create:true,exclusive:false},
函数gotFileEntry(fileEntry){
var sPath=fileEntry.fullPath.replace(“dummy.html”,”);
var fileTransfer=new fileTransfer();
fileEntry.remove();
fileTransfer.download(
"https://dl.dropbox.com/u/xxxxx/index_2.html",
sPath+“index_aerosoft.html”,
函数(文件){
log(“下载完成:+theFile.toURI());
showLink(theFile.toURI());
},
函数(错误){
log(“下载错误源”+错误源);
log(“下载错误目标”+错误目标);
日志(“上传错误代码:+错误代码”);
}
);
},
失败);
}, 
失败);
}
函数showLink(url){
警报(url);
var divEl=document.getElementById(“就绪”);
var aElem=document.createElement(“a”);
aElem.setAttribute(“目标”,“空白”);
setAttribute(“href”,url);
appendChild(document.createTextNode(“准备好了!单击打开”))
附属物儿童(aElem);
}
功能失效(evt){
log(evt.target.error.code);
}
函数ondevicerady()
{
下载文件();
}

正在下载文件…

我可以下载并访问该文件,但我可以在Phonegap中的某个位置设置下载到“www”文件夹的路径吗? 或者如何确定文件的路径(以便链接到该路径)

Xcode控制台告诉我
file://localhost/var/mobile/Applications/1AE34410-C57E-4896-8616-042E386552E0/Documents/index_2.html


我可以链接到它吗?

您不能替换源代码中的代码…
因为android资产文件夹是只读的 你在那里写的html。。。 如果您可以编程java,我建议将html文件从资产复制到SD卡,然后从那里运行代码
你可以修改它

但我在phonegap构建(在dreamweaver中)中做到了这一点,它对我起了作用:

// retrive html data as String in Var : MY_Data
window.localStorage.setItem("mydiv_update", My_Data);
document.getElementById("your_div").innerHtml = My_Data;
然后每次加载页面时,检查
window.localStorage.getItem(“mydiv\u update”)
是否存在。如果是,则更改div的innetHTML。像这样:

if(window.localStorage.getItem("mydiv_update")){
  document.getElementById("your_div").innerHtml = window.localStorage.getItem("mydiv_update");
}
不要忘记存储权限:

权限


Android

app/res/xml/config.xml:

<plugin name="File" value="org.apache.cordova.FileUtils" />
<plugin name="FileTransfer" value="org.apache.cordova.FileTransfer" />
<plugin name="File" value="CDVFile" />
<plugin name="FileTransfer" value="CDVFileTransfer" />  

app/AndroidManifest.xml:

<uses-permission android:name="android.permission.WRITE_EXTERNAL_STORAGE" />


Bada

不需要任何权限


BlackBerry WebWorks

www/plugins.xml:

<plugin name="File" value="org.apache.cordova.file.FileManager" />
<plugin name="FileTransfer" value="org.apache.cordova.http.FileTransfer" />

www/config.xml:

<feature id="blackberry.io.file" required="true" version="1.0.0.0" />
<feature id="blackberry.utils"   required="true" version="1.0.0.0" />
<feature id="blackberry.io.dir"  required="true" version="1.0.0.0" />
<rim:permissions>
    <rim:permit>access_shared</rim:permit>
</rim:permissions>  

访问共享

iOS:
config.xml:

<plugin name="File" value="org.apache.cordova.FileUtils" />
<plugin name="FileTransfer" value="org.apache.cordova.FileTransfer" />
<plugin name="File" value="CDVFile" />
<plugin name="FileTransfer" value="CDVFileTransfer" />  


网络操作系统:
不需要任何权限


Windows Phone:

不需要任何权限。

您无法替换源代码中的代码…
因为android资产文件夹是只读的 你在那里写的html。。。 如果您可以编程java,我建议将html文件从资产复制到SD卡,然后从那里运行代码
你可以修改它

但我在phonegap构建(在dreamweaver中)中做到了这一点,它对我起了作用:

// retrive html data as String in Var : MY_Data
window.localStorage.setItem("mydiv_update", My_Data);
document.getElementById("your_div").innerHtml = My_Data;
然后每次加载页面时,检查
window.localStorage.getItem(“mydiv\u update”)
是否存在。如果是,则更改div的innetHTML。像这样:

if(window.localStorage.getItem("mydiv_update")){
  document.getElementById("your_div").innerHtml = window.localStorage.getItem("mydiv_update");
}
不要忘记存储权限:

权限


Android

app/res/xml/config.xml:

<plugin name="File" value="org.apache.cordova.FileUtils" />
<plugin name="FileTransfer" value="org.apache.cordova.FileTransfer" />
<plugin name="File" value="CDVFile" />
<plugin name="FileTransfer" value="CDVFileTransfer" />  

app/AndroidManifest.xml:

<uses-permission android:name="android.permission.WRITE_EXTERNAL_STORAGE" />