[英]File Download at front end using angular js
On click of download button beside name of a file I am getting the file content from backend . 在单击文件名旁边的下载按钮时,我正在从后端获取文件内容。 This content is in the bytes . 此内容以字节为单位。 The file can be of any type . 该文件可以是任何类型。 How to convert the data received from backend to file and download it automatically on receiving response(file Content) .New bee in html and angular js .Any pointers or suggestions needed :) 如何将从后端接收的数据转换为文件,并在接收到响应(文件内容)时自动下载。HTML和Angular JS中的新蜜蜂。需要任何指针或建议:)
You will need to have your backend tell your front-end the location of the file, and then the front end can place a link to the file. 您将需要让后端告知您的前端文件的位置,然后前端才能放置指向该文件的链接。 The backend should probably generate a unique hash name for this file. 后端可能应该为此文件生成一个唯一的哈希名称。
The actual file can be returned as part of a Rest GET request as long as the backend webserver has the correct mime types configured. 只要后端Web服务器配置了正确的mime类型,就可以将实际文件作为Rest GET请求的一部分返回。
So in your controller you would call a service to get the file path: 因此,在控制器中,您将调用服务以获取文件路径:
SomeController.$inject = ['$http'];
var SomeController = function($http) {
var self = this;
$http.get('/download-file-path').then(function(path) {
self.path = path;
}
}
Then in your view 然后在您看来
<div ng-controller='SomeController as vm'>
<a ng-href="{{vm.path}}">Download</a>
</div>
When angular calls GET: /download-file-path
the backend should return the name and path of the file, say something like /download/file-7dje79ae.xml
. 当角度调用GET: /download-file-path
,后端应返回文件的名称和路径,例如/download/file-7dje79ae.xml
。 Then angular puts this path in the a
link. 然后放角这条道路的a
环节。 When the user clicks the download button, the users browser will then make a request to /download/file-7dje79ae.xml
and download the file. 当用户单击下载按钮时,用户浏览器将向/download/file-7dje79ae.xml
发出请求并下载文件。
声明:本站的技术帖子网页,遵循CC BY-SA 4.0协议,如果您需要转载,请注明本站网址或者原文地址。任何问题请咨询:yoyou2525@163.com.