使用Angular2 -Ionic 2下载文件



我有一个离子2应用程序,我需要从SoundCloud下载音频。我已经有我需要执行请求的URL:

let url = `https://api.soundcloud.com/tracks/${audio.id}/download?client_id=${this.SC_CLIENT_ID}`

我想知道该怎么做。我尝试了Filetransfer:

public download(audio: any): void {
        this.platform.ready().then(() => {
            console.log("Clickeo para descargar: " + audio.id);
            let url = `https://api.soundcloud.com/tracks/${audio.id}/download?client_id=${this.SC_CLIENT_ID}`;
            let pathToSaveTo: string = '';
            if (this.platform.is('android')) {
                pathToSaveTo = cordova.file.externalApplicationStorageDirectory + audio.id + '.wav';
                let fileTransfer = new Transfer();
                fileTransfer.onProgress(this.onProgress);
                fileTransfer.download(url, pathToSaveTo)
                    .then((entry) => {
                        console.log('download complete: ' + entry.toURL());
                    }, (error) => {
                        let prompt = this.alertCtrl.create({
                            title: 'Error',
                            subTitle: error,
                            buttons: ['Accept']
                        });
                        prompt.present();
                    });
            }
        });
    }
    onProgress = (progressEvent: ProgressEvent) : void => {
        this.ngZone.run(() => {
            if (progressEvent.lengthComputable) {
                let progress: any = Math.round((progressEvent.loaded / progressEvent.total) * 100);
                console.log(progress);
                let toast = this.toastCtrl.create({
                  message: progress,
                  duration: 100
                });
                toast.present();
            }
        });
    }

,但它总是下载零KB文件。有解决方案吗?也许是Angular2方法,还是我做错了?如果我将此URL放入浏览器,使用正确的音频ID和MI客户端ID,则开始下载,但不会使用Filetransfer。

谢谢。

ivan。

我的问题是SoundCloud返回了重定向的响应。我必须检测到重定向状态,然后手动重定向到response.headers.Location

这是工作代码:

public download(localAudio: LocalAudio): void {
    let fileName: string = localAudio.id + '.mp3';
    var audio: StorageAudio = {
        localAudio: localAudio,
        url: `https://api.soundcloud.com/tracks/${localAudio.id}/download?client_id=${this.SC_CLIENT_ID}`,
        fileName: fileName,
        filePath: this.file.dataDirectory + fileName
    };
    if (this.platform.is('ios')) {
        audio.filePath = audio.filePath.replace(/^file:///, '');
    }
    this.toastNotifications.showStartingDownloadMsg();
    this.http.downloadFile(audio.url, {}, {}, audio.filePath).then(response => {
        this.toastNotifications.showDownloadCompleteMsg();
        this.storeAudioInformation(audio);
    }).catch(response => {
        /**
         * Handle redirects if exist.
         */
        if (this.hasRedirection(response)) {
            console.log("Redirect to " + response.headers.Location);
            audio.url = response.headers.Location;
            this.redirectToDownload(audio);
        } else {
            this.toastNotifications.showDownloadErrorMsg();
        }
    });
}
public hasRedirection(response: any): boolean {
    return (response.headers.Location && (response.status === 301 || response.status === 302));
}
public redirectToDownload(audio: StorageAudio): void {
    this.http.downloadFile(audio.url, {}, {}, audio.filePath).then(response => {
        this.toastNotifications.showDownloadCompleteMsg();
        this.storeAudioInformation(audio);
    }).catch(response => {
        console.log("Error catch: " + JSON.stringify(response));
    });
}

相关内容

  • 没有找到相关文章

最新更新