Cordova 使用angular2-ionic 2下载文件
我有一个Ionic 2应用程序,我需要从soundcloud下载音频。我已经有了执行请求所需的url:Cordova 使用angular2-ionic 2下载文件,cordova,angular,ionic2,file-transfer,angular2-http,Cordova,Angular,Ionic2,File Transfer,Angular2 Http,我有一个Ionic 2应用程序,我需要从soundcloud下载音频。我已经有了执行请求所需的url: let url = `https://api.soundcloud.com/tracks/${audio.id}/download?client_id=${this.SC_CLIENT_ID}` 我想知道怎么做。我尝试了文件传输: public download(audio: any): void { this.platform.ready().then(() =>
let url = `https://api.soundcloud.com/tracks/${audio.id}/download?client_id=${this.SC_CLIENT_ID}`
我想知道怎么做。我尝试了文件传输:
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的文件。有解决办法吗?也许是安格拉2路,还是我做错了?
若我将这个url放在浏览器中,使用正确的音频id和mi客户端id,它将开始下载,但不会使用文件传输
谢谢你,这是事先准备好的
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));
});
}
我的问题是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));
});
}
参考这个:在MELWINVINCENT为你工作,但这不是我的问题。我的问题是soundcloud返回了带有重定向的响应。我必须检测重定向状态并手动重定向到
响应.headers.Location
参考以下内容:在MELWINVINCENT为我工作,但这不是我的问题。我的问题是soundcloud返回了带有重定向的响应。我必须检测重定向状态并手动重定向到response.headers.Location
我还试图在Ionic应用程序中使用soundcloud url下载。当我尝试使用您的解决方案时,我得到了response.headers.Location=未定义,因此我无法下载。你对此有什么想法吗?我也在尝试在爱奥尼亚应用程序中使用SoundCloudURL下载。当我尝试使用您的解决方案时,我得到了response.headers.Location=未定义,因此我无法下载。你知道这件事吗?