我正在使用Angular js来显示加载屏幕。它适用于除REST服务之外的所有REST服务调用来下载文件。我理解它为什么不工作,因为对于下载,我没有使用$resource进行任何服务调用;相反,我使用普通的方法来下载文件,因此Angular js代码对启动/完成服务请求没有任何控制。我试图使用$resource来访问这个REST服务,但是我是从这个服务中获取数据的,在这种情况下,加载屏幕工作得很好,但是不确定如何使用这些数据来显示给用户,以便以角度方式下载。以下是必需的详细信息。请帮帮忙。
使用iframe方法的方法1:
/*Download file */
scope.downloadFile = function (fileId) {
//Show loading screen. (Somehow it is not working)
scope.loadingProjectFiles=true;
var fileDownloadURL = "/api/files/" + fileId + "/download";
downloadURL(fileDownloadURL);
//Hide loading screen
scope.loadingProjectFiles=false;
};
var $idown; // Keep it outside of the function, so it's initialized once.
var downloadURL = function (url) {
if ($idown) {
$idown.attr('src', url);
} else {
$idown = $('<iframe>', { id: 'idown', src: url }).hide().appendTo('body');
}
};
使用$resource的方法2(不确定如何在屏幕上显示要下载的数据)
/*Download file */
scope.downloadFile = function (fileId) {
//Show loading screen (Here loading screen works).
scope.loadingProjectFiles=true;
//File download object
var fileDownloadObj = new DownloadFile();
//Make server call to create new File
fileDownloadObj.$get({ fileid: fileid }, function (response) {
//Q? How to use the response data to display on UI as download popup
//Hide loading screen
scope.loadingProjectFiles=false;
});
};
发布于 2015-11-16 16:33:19
这是$resource服务的正确模式:
scope.downloadFile = function (fileId) {
//Show loading screen (Here loading screen works).
scope.loadingProjectFiles=true;
var FileResource = $resource('/api/files/:idParam', {idParam:'@id'});
//Make server call to retrieve a file
var yourFile = FileResource.$get({ id: fileId }, function () {
//Now (inside this callback) the response data is loaded inside the yourFile variable
//I know it's an ugly pattern but that's what $resource is about...
DoSomethingWithYourFile(yourFile);
//Hide loading screen
scope.loadingProjectFiles=false;
});
};
我同意您的看法,这是一种奇怪的模式,与其他API不同,在其他API中,下载的数据被分配给回调函数中的参数,因此您会感到困惑。
注意参数的名称和大小写,注意这里涉及到两个映射,一个是$resource对象的调用者和对象本身之间的映射,另一个是该对象和它构造的用于下载实际数据的url之间的映射。
发布于 2013-11-27 21:36:34
下面是第二种方法的一些想法,你可以在下载完成后向用户显示一个链接:
上找到一些灵感
https://stackoverflow.com/questions/19099297
复制相似问题