Phonegap下载html文件并替换它(来源) [英] Phonegap Download html File and replace it (source)

查看:115
本文介绍了Phonegap下载html文件并替换它(来源)的处理方法,对大家解决问题具有一定的参考价值,需要的朋友们下面随着小编来一起学习吧!

问题描述

我试图找出如何用一个下载的文件替换或添加一些Phonegap的源文件。

 <!DOCTYPE html> 
< html>
< head>
< title>< / title>
< script type =text / javascriptcharset =utf-8src =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>
下载文件...< br />
< span id =ready>< / span>
< / p>
< / body>
< / html>

我可以下载并访问该文件,但可以在某处设置下载到www文件夹在Phonegap?
或者我怎么知道文件的路径(所以我可以链接到那个路径)



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


解决方案

您不能替换源代码...

因为android资源文件夹是只读
和你写的地方的html你...
如果你可以编程java我建议应对html文件从资产到sdcard然后运行代码从那里

你可以修改它然后



但我做了这里phonegap-build(在dreamweaver),它为我工作:

  //在Var中检索html数据为String:MY_Data 
window.localStorage.setItem(mydiv_update,My_Data);
document.getElementById(your_div)。innerHtml = My_Data;

然后每次页面加载检查 window.localStorage.getItem mydiv_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:

 code>< plugin name =Filevalue =org.apache.cordova.FileUtils/> 
< plugin name =FileTransfervalue =org.apache.cordova.FileTransfer/>

app / AndroidManifest.xml:

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






Bada p>

不需要权限。






BlackBerry WebWorks



www / plugins.xml:

  plugin name =Filevalue =org.apache.cordova.file.FileManager/> 
< plugin name =FileTransfervalue =org.apache.cordova.http.FileTransfer/>

www / config.xml:

 < feature id =blackberry.io.filerequired =trueversion =1.0.0.0/> 
< feature id =blackberry.utilsrequired =trueversion =1.0.0.0/>
< feature id =blackberry.io.dirrequired =trueversion =1.0.0.0/>
< rim:permissions>
< rim:permit> access_shared< / rim:permit>
< / rim:permissions>






iOS: br>
config.xml:

 < plugin name =Filevalue =CDVFile/> 
< plugin name =FileTransfervalue =CDVFileTransfer/>






webOS: br>
不需要权限。






Windows Phone:

不需要权限。 p>

i'm tring to figure out how to replace or add some of the source files of Phonegap with the a downloaded file.

<!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>

I can download and access the file but can i somewhere set the path of download to the 'www' folder in Phonegap? Or how i figure out what the the path of the file (so i can link to that path)

Xcode Console tells me file://localhost/var/mobile/Applications/1AE34410-C57E-4896-8616-042E386552E0/Documents/index_2.html

Can i somehow link to that?

解决方案

you can't replace the code in source ...
because the android asset folder is read-only and the html you have written places there ... if you can program java i suggest coping html files from assets to sdcard then running the code from there
you can modify it then

but i did this in phonegap-build (in dreamweaver) and it worked for me :

// retrive html data as String in Var : MY_Data
window.localStorage.setItem("mydiv_update", My_Data);
document.getElementById("your_div").innerHtml = My_Data;

then each time the page is loading check if window.localStorage.getItem("mydiv_update") exists. if yes then change the div's innetHTML . like this :

if(window.localStorage.getItem("mydiv_update")){
  document.getElementById("your_div").innerHtml = window.localStorage.getItem("mydiv_update");
}

and don't forget storage permissions :

Permissions


Android

app/res/xml/config.xml :

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

app/AndroidManifest.xml :

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


Bada

No permissions are required.


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="CDVFile" />
<plugin name="FileTransfer" value="CDVFileTransfer" />  


webOS :
No permissions are required.


Windows Phone :
No permissions are required.

这篇关于Phonegap下载html文件并替换它(来源)的文章就介绍到这了,希望我们推荐的答案对大家有所帮助,也希望大家多多支持IT屋!

查看全文
登录 关闭
扫码关注1秒登录
发送“验证码”获取 | 15天全站免登陆