1

我尝试在 Cordova 中本地存储并显示我使用 Ajax 调用检索到的图像。这是我的来源:

<!DOCTYPE html>
<!--
    Licensed to the Apache Software Foundation (ASF) under one
    or more contributor license agreements.  See the NOTICE file
    distributed with this work for additional information
    regarding copyright ownership.  The ASF licenses this file
    to you under the Apache License, Version 2.0 (the
    "License"); you may not use this file except in compliance
    with the License.  You may obtain a copy of the License at

    http://www.apache.org/licenses/LICENSE-2.0

    Unless required by applicable law or agreed to in writing,
    software distributed under the License is distributed on an
    "AS IS" BASIS, WITHOUT WARRANTIES OR CONDITIONS OF ANY
     KIND, either express or implied.  See the License for the
    specific language governing permissions and limitations
    under the License.
-->
<html>
    <head>
        <meta charset="utf-8" />
        <meta name="format-detection" content="telephone=no" />
        <meta name="viewport" content="user-scalable=no, initial-scale=1, maximum-scale=1, minimum-scale=1, width=device-width, height=device-height, target-densitydpi=device-dpi" />
        <link rel="stylesheet" type="text/css" href="css/index.css" />

        <title>Test requestFileSystem</title>
    </head>
    <body>
        <div class="app">
            <h1>Apache Cordova</h1>
            <div id="deviceready" class="blink">
                <p class="event listening">Connecting to Device</p>
                <p class="event received">Device is Ready</p>
            </div>
        </div>
        <script type="text/javascript" src="cordova.js"></script>
        <script type="text/javascript" src="js/index.js"></script>
        <script type="text/javascript">
            app.initialize();
        </script>

    </body>
</html>
<script>
    window.requestFileSystem  = window.requestFileSystem || window.webkitRequestFileSystem;

    function onError(e) {
        console.log('Error', e);
    }

    var xhr = new XMLHttpRequest();
    xhr.open('GET', 'http://upload.wikimedia.org/wikipedia/fr/2/26/10_francs_Mathieu_1987_F365-28_revers.jpg', true);
    xhr.responseType = 'blob';

    window.onload = function() {
        document.addEventListener("deviceready", start, false);
    }

    function start() {
        alert("dans start : deviceready fired");
        xhr.send();
    }

    xhr.onload = function(e) {
        alert("xhr.onload");
        doProcess();
    }

    function doProcess() {
        alert('dans doProcess');
        window.requestFileSystem(PERSISTENT, 1024 * 1024, function(fs) { // here
            alert('dans requestFileSystem 1 success');
            fs.root.getFile('image.jpg', {create: true}, function(fileEntry) {
                alert('dans getFile 1 success');
                fileEntry.createWriter(function(writer) {
                    alert('dans createWriter');
                    writer.onwrite = function(e) {};
                    writer.onerror = function(e) {};

                    var blob = new Blob([xhr.response], {type: 'image/jpeg'});

                    writer.write(blob);

                }, function(e) {
                    console.log('Error', e);
                });
            }, function(e) {
                console.log('Error', e);
            });
        }, function(e) {
            alert('error');
            console.log('Error', e);
        });

        window.requestFileSystem(PERSISTENT, 1024 * 1024, function(fs) {
            alert('dans reqestFileSystem 2 success');
            fs.root.getFile('image.jpg', {create: false}, function(fileEntry) {
                fileEntry.file(function(file) {
                    alert('dans file');
                    var reader = new FileReader();
                    reader.onloadend = function(event) {
                        var img = document.createElement("img");
                        img.src = event.target.result;

                        document.body.parentNode.insertBefore(img, document.body.nextSibling);
                    };
                    reader.readAsDataURL(file);
                }, function(e) {
                    console.log('Error', e);
                });
            }, function(e) {
                console.log('Error', e);
            });
        }, function(e) {
            console.log('Error', e);
        });

    };

    //xhr.send();</script>

问题是,似乎从未调用过第一个 window.requestFileSystem 调用(注释为“here”)的成功函数。也没有调用错误函数。显示我的“dans doProcess”警报,然后什么也没有发生。我正在使用 XCode 和 iOS 模拟器进行测试。任何想法?

4

0 回答 0