javascript - ionic download file, save to temporary filesystem and open with default app -


i've searched far , wide couldn't find appropriate answer use-case. i'd download base64-encoded file (could pdf, pgn, jpeg - using pdf in following example) backend, save temporary filesystem folder, open - possibly using appropriate app on device, if present. let /file route served following asp.net mvc webapi controller:

public class filecontroller : apicontroller {     // post api/file/     public httpresponsemessage post([frombody]string fullpath)     {         httpresponsemessage result = new httpresponsemessage(httpstatuscode.ok);         var stream = new filestream(fullpath, filemode.open);         result.content = new streamcontent(stream);         result.content.headers.contenttype = new mediatypeheadervalue("application/octet-stream");         return result;     }  } 

i came following angularjs script:

$scope.download = function(filename) {     window.requestfilesystem(window.temporary, 1024*1024*500, function(filesystem) {         filesystem.root.getdirectory("tempdir", {create: true}, function(direntry) {             direntry.getfile(filename, {create: true, exclusive: false}, function(fileentry) {                 $http.post("/file", json.stringify(filename), {                     headers: {accept: "application/octet-stream"}                 }).success(function(res) {                     fileentry.createwriter(function(fileentrycontent) {                         var blob = new blob([res], {type: "application/pdf"});                         fileentrycontent.write(blob);                         //now load file                         $scope.load(filename);                     });                 }).error(function(err) {                     console.warn(err);                 });             }, function(err) {                 console.warn("getfile failed:", err);             });         }, function(err) {             console.warn("getdirectory failed:", err);         });     }, function(err) {         console.warn("requestfilesystem failed:", err);     }); };  $scope.download("foo.pdf");  $scope.load = function(filename) {     window.requestfilesystem(window.temporary, 1024*1024*500, function(filesystem) {         filesystem.root.getdirectory("tempdir", {create: false}, function(direntry) {             direntry.getfile(filename, {create: false, exclusive: false}, function(fileentry) {                 //this magic needs happen!             }, function(err) {                 console.warn("getfile failed:", err);             });         }, function(err) {             console.warn("getdirectory failed:", err);         });     }, function(err) {         console.warn("requestfilesystem failed:", err);     }); }; 

i'm stomped @ loading phase: tried window.opening base64-encoded content of file, http.getting fileentry.tourl() nothing seems work. checked out cordova's file opener 2 plugin seems can open files stored on device's sdcard or such. clue welcomed! cheers.

fileopener2 plugin pretty you're option think - , can work in scenario. you'll need make sure file saved outside app's container on device, other apps cannot access this. can find file structure each platform, , is/isn't public, on plugin page. you'll need save different locations depending on platform. works me:-

if (ionic.platform.isios()) storagepath = cordova.file.cachedirectory + "/temp"; else if(ionic.platform.isandroid()) storagepath = cordova.file.externalrootdirectory + "/yourapp/temp"; 

you can use storagepath base targetpath when downloading. highly recommend using ngcordova. sample below partially based on i'm using on ios , android haven't tested

// add options here, i.e. headers need add request var options = {}; $cordovafiletransfer.download(url, targetpath, options, true).then(function(result) {     //on successful transfer try , extract file object     result.file(function (file) {         var localfile = file.localurl;         resolvelocalfilesystemurl(localfile, function (entry) {             var nativepath = entry.tourl();             // open in app, fail if app doesn't exist can open mime type             $cordovafileopener2.open(nativepath, mime).then(function () {                // success!             }, function (err) {                // error!             });         }, function(error) {             //handle error here         });        }, function (error) {         // handle error here      });         }, function (err) {     //handle error here }); 

Comments

Popular posts from this blog

javascript - jQuery: Add class depending on URL in the best way -

caching - How to check if a url path exists in the service worker cache -

Redirect to a HTTPS version using .htaccess -