Phonegap/Cordova 3.6 - Download of file through blob:file

流过昼夜 提交于 2019-12-04 18:28:40

Okay. blob: requires special additions to the whitelist system, of which you seem to have an incomplete understanding _OR_ are using outdated documentation. I can tell because launch-external="yes" has been removed from use.

NOTE DOING THIS WILL MAKE YOUR APP INSECURE. IT IS UP TO YOU TO SECURE YOUR APP.
Add this to your config.xml

<plugin name="cordova-plugin-whitelist" source="npm" spec="1.1.0" />
<allow-navigation href="*" />
<allow-intent href="*" />
<access origin="*" /> <!-- Required for iOS9 -->

NOTE YOUR APP IS NOW INSECURE. IT IS UP TO YOU TO SECURE YOUR APP. Add the following to your index.html and every .html page.

<meta http-equiv="Content-Security-Policy" 
         content="default-src * blob:; 
                  style-src * 'self' 'unsafe-inline' 'unsafe-eval'; 
                  script-src * 'self' 'unsafe-inline' 'unsafe-eval';">

I have added blob: into the CSP definition. However, this just opens up the app to allow the blob: URL. It is still not clear to me what you mean when you say "download the file", as blob: is not transport protocol. It is only intended to define a type of file.

I also recommend reading this whitelist worksheet, make sure to read 9. CSP (Content Security Policy) Lastly, if you want to continue this discussion, please continue on Google Groups

So I created a solution that saves the blob and then opens it with the system viewer.
If you want it to be saved in a different location see: here.

By choosing this location you can have it download straight to the desired location.
If you open the file with the system viewer then there is often an option for the user to save the file.

// previously blob is defined as file, 
//and file name is saved in data.FileName

var savedFile;

window.resolveLocalFileSystemURL( cordova.file.cacheDirectory, function( dir ) {

    dir.getFile( data.FileName, { create:true }, function( file_ ) {

        savedFile = file_;
        saveFile();

    });
});

function saveFile(str) {
    if( !savedFile ) return;

    savedFile.createWriter(function(fileWriter) {

        fileWriter.write( file );
        cordova.plugins.disusered.open( savedFile.nativeURL );
        console.log( "file " + savedFile.nativeURL + " opened" );

    }, function( e ){
        throw( 'createWriter Error error' + JSON.stringify( e ) );
    });
}

This uses the plugins of:

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