gpt4 book ai didi

cordova - 使用 angular2 - ionic 2 下载文件

转载 作者:太空狗 更新时间:2023-10-29 19:34:46 26 4
gpt4 key购买 nike

我有一个 Ionic 2 应用程序,我需要从 soundcloud 下载音频。我已经有了执行请求所需的网址:

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。

提前致谢。

伊万。

最佳答案

我的问题是 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));
});
}

关于cordova - 使用 angular2 - ionic 2 下载文件,我们在Stack Overflow上找到一个类似的问题: https://stackoverflow.com/questions/42651624/

26 4 0
Copyright 2021 - 2024 cfsdn All Rights Reserved 蜀ICP备2022000587号
广告合作:1813099741@qq.com 6ren.com