Ios Cordova/Ionic-从InApp浏览器下载文件

Ios Cordova/Ionic-从InApp浏览器下载文件,ios,cordova,inappbrowser,Ios,Cordova,Inappbrowser,场景是这样的:我在InAppBrowser中打开一个网站,用户在那里完成工作后,该网站生成一个.pdf供用户下载,问题是pdf没有下载,而是在浏览器中打开 有没有办法让它从iApp浏览器下载?我目前正在开发一款iOS应用程序,因此解决方案更适合iOS 提前感谢。以下是@jcesarmobile的建议这是我想到的: 首先,我必须安装 打开网页 在windowref上为loadstart事件创建一个侦听器,并检查正在加载的是否是pdf(这是我的情况)。 创建用于处理文件下载的函数,然后将其打开: 我

场景是这样的:我在InAppBrowser中打开一个网站,用户在那里完成工作后,该网站生成一个.pdf供用户下载,问题是pdf没有下载,而是在浏览器中打开

有没有办法让它从iApp浏览器下载?我目前正在开发一款iOS应用程序,因此解决方案更适合iOS


提前感谢。

以下是@jcesarmobile的建议这是我想到的:

首先,我必须安装

打开网页 在
windowref
上为
loadstart
事件创建一个侦听器,并检查正在加载的是否是pdf(这是我的情况)。 创建用于处理文件下载的函数,然后将其打开: 我现在面临的问题是它的下载路径,我就是无法打开它。不过,至少现在文件已经下载了。我必须创建一个localStorage项来保存不同文件的路径

这一步中缺少许多验证,这只是我快速制作的一个示例,用于检查它是否有效。需要进一步验证

  • 使用IAB插件打开窗口并添加事件侦听器 ref=窗口打开(url为“空白”); 参考addEventListener('loadstop',loadStopCallBack)

  • 在InAppBrowser窗口中,使用https://xxx.pdf“>文档名

  • 实现loadStopCallBack函数

    function loadStopCallBack(refTemp) {
        if(refTemp.url.includes('downloadDoc')) {
            rtaParam = getURLParams('downloadDoc', refTemp.url);
    
            if(rtaParam != null)
                downloadFileFromServer(rtaParam);
            return;
        }
    }
    
    function getURLParams( name, url ) {
        try {
            if (!url)
                url = location.href;
            name = name.replace(/[\[]/, "\\\[").replace(/[\]]/, "\\\]");
            var regexS = "[\\?&]" + name + "=([^&#]*)";
            var regex = new RegExp(regexS);
            var results = regex.exec(url);
            return results == null ? null : results[1];
        } catch (e) {
            showSMS(e);
            return null;
        }
    }
    
  • 创建下载方法之后

    function downloadFileFromServer(fileServerURL){
    try {
        var Downloader = window.plugins.Downloader;
        var fileName = fileServerURL.substring(fileServerURL.lastIndexOf("/") + 1);
    
        var downloadSuccessCallback = function(result) {
              console.log(result.path); 
    
        };
    
        var downloadErrorCallback = function(error) {
            // error: string
            console.log(error);
        };
    
        //TODO cordova.file.documentsDirectory for iOS
    
        var options = {
            title: 'Descarga de '+ fileName, // Download Notification Title
            url: fileServerURL, // File Url
            path: fileName, // The File Name with extension
            description: 'La descarga del archivo esta lista', // Download description Notification String
            visible: true, // This download is visible and shows in the notifications while in progress and after completion.
            folder: "Download" // Folder to save the downloaded file, if not exist it will be created
        };
    
        Downloader.download(options, downloadSuccessCallback, downloadErrorCallback);
    } catch (e) {
        console.log(e);
    }
    
    }

    你可以在这里获得插件


    如果用户被重定向到一个.pdf文件或与用户工作时的url不同的url,则可以使用loadstart事件检测.pdf的url,然后使用filetransfer插件下载,或者使用带有_系统选项的inAppBrowser打开safari,safari将显示一个“打开“用户可以使用任何支持pdf文件的应用程序打开pdf的消息网站提供了一个下载.pdf文件的按钮,当用户触摸该按钮时,pdf将在应用浏览器中打开。”。我将尝试添加
    loadstart
    事件解决方案并返回给您。我想如果它起作用的话,我还必须处理查看pdf的窗口。感谢您的建议。如果您在loadstart上检测到pdf url,则可以使用ref.close();启动应用程序后关闭inAppBrowser窗口的步骤download@João Pimentel Ferreira为什么编辑会将$timeout更改为setTimeout$timeout是angularjs服务,是setTimeout fn的包装,但在angularjs的作用域内运行。啊,好吧,这是因为这在纯Cordova中不可用,
    setTimeout
    是纯香草JavascriptYeah,但这不是香草js,它是ionic1,它使用angularjs,因此$timeout而不是setTimeout:)好的,但爱奥尼亚是基于科尔多瓦的,科尔多瓦有许多项目不使用爱奥尼亚
    function downloadReceipt(args) {
      var fileTransfer = new FileTransfer();
      var uri = encodeURI(args.url);
    
      fileTransfer.download(
        uri, // file's uri
        args.targetPath, // where will be saved
        function(entry) {
          console.log("download complete: " + entry.toURL());
          window.open(entry.toURL(), '_blank', 'location=no,closebuttoncaption=Cerrar,toolbar=yes,enableViewportScale=yes');
        },
        function(error) {
          console.log("download error source " + error.source);
          console.log("download error target " + error.target);
          console.log("upload error code" + error.code);
        },
        true,
        args.options
      );
    }
    
    function loadStopCallBack(refTemp) {
        if(refTemp.url.includes('downloadDoc')) {
            rtaParam = getURLParams('downloadDoc', refTemp.url);
    
            if(rtaParam != null)
                downloadFileFromServer(rtaParam);
            return;
        }
    }
    
    function getURLParams( name, url ) {
        try {
            if (!url)
                url = location.href;
            name = name.replace(/[\[]/, "\\\[").replace(/[\]]/, "\\\]");
            var regexS = "[\\?&]" + name + "=([^&#]*)";
            var regex = new RegExp(regexS);
            var results = regex.exec(url);
            return results == null ? null : results[1];
        } catch (e) {
            showSMS(e);
            return null;
        }
    }
    
    function downloadFileFromServer(fileServerURL){
    try {
        var Downloader = window.plugins.Downloader;
        var fileName = fileServerURL.substring(fileServerURL.lastIndexOf("/") + 1);
    
        var downloadSuccessCallback = function(result) {
              console.log(result.path); 
    
        };
    
        var downloadErrorCallback = function(error) {
            // error: string
            console.log(error);
        };
    
        //TODO cordova.file.documentsDirectory for iOS
    
        var options = {
            title: 'Descarga de '+ fileName, // Download Notification Title
            url: fileServerURL, // File Url
            path: fileName, // The File Name with extension
            description: 'La descarga del archivo esta lista', // Download description Notification String
            visible: true, // This download is visible and shows in the notifications while in progress and after completion.
            folder: "Download" // Folder to save the downloaded file, if not exist it will be created
        };
    
        Downloader.download(options, downloadSuccessCallback, downloadErrorCallback);
    } catch (e) {
        console.log(e);
    }