I can't open local files from Filesystem API on my Android phone

偶尔善良 提交于 2019-12-13 20:43:18

问题


I have made an HTML 5 application that downloads a pdf file on my server, and writes it in the browser sandbox system.
When I go to filesystem:myServerUrl/persistent, I see my pdf file with the correct size. I click on it, and there is a "download started" notification, but about one second later, I get a download error (Reason 404 when I look into the Android logs).
I tried with a Download. But I have the same error.
Note that it is working fine on my computer, and when I try with an image, I can display it directly (just clicking on the file name), but cannot download it.
I can't find any piece of information anywhere, am I missing something ?

How I download my file :

    function fetchResource() {
        var xhr = new XMLHttpRequest();
        xhr.open("GET", resourceURL, true);
        xhr.responseType = "arraybuffer";
        xhr.onload = function(e) {
            console.log("xhr responded");
            if (this.status == 200) {
                console.log("status 200");
                writeFile(this.response);
            }
        }
        xhr.send();
        console.log("xhr sent");
    }

How I write my file :

    function writeFile(content){
        console.log("writeFile");
        fs1.root.getFile('test2.pdf', {create: true}, function(fileEntry) {

            // Create a FileWriter object for our FileEntry (log.txt).
            fileEntry.createWriter(function(fileWriter) {

              fileWriter.onwriteend = function(e) {
                console.log('Write completed.');
                alert("completed");
              };

              fileWriter.onerror = function(e) {
                console.log('Write failed: ' + e.toString());
                alert("erreur: "+e.toString());
              };

             var blob = new Blob([content]);                  
              fileWriter.write(blob);

            }, errorHandler);

        });
    }

Update: the exact error message in English is
<Untitled>
Download unsuccessful

Also, when I display a local image, and I long-click it, the option "Save Image" is disabled. Could it be intented? Why?


回答1:


I have just tested this on Stable, Beta. And it kind of works.

Stable (27) has a problem where it can't download client generated files. For example if you use the a[download] attribute you will see that it opens up the downloader but just sits there doing nothing.

In the Beta (28) everything works as expected, so I would expect your pdf to also save.

I simplified your code here: http://jsbin.com/opavet/latest which works for both links on Beta. But only the later on stable.



来源:https://stackoverflow.com/questions/17239264/i-cant-open-local-files-from-filesystem-api-on-my-android-phone

易学教程内所有资源均来自网络或用户发布的内容,如有违反法律规定的内容欢迎反馈
该文章没有解决你所遇到的问题?点击提问,说说你的问题,让更多的人一起探讨吧!