简体   繁体   English

使用angular js在前端下载文件

[英]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.

相关问题 Angular JS-前端Web开发框架? - Angular JS - Front end web development framework? 使用Axios将Vue前端的pdf文件上传/下载到Spring Backend - Upload/Download pdf file from Vue front-end to Spring Backend using Axios 如何通过单击链接在 Angular Front End Web App 的浏览器中下载或保存 .csv 文件? 浏览器投诉没有文件 - How can I download or save a .csv file in browser in Angular Front End Web App on click of a link? browser complaints no file 在前端使用 Python 枕头 Angular - Using Python Pillow in front-end Angular 要使用ng2文件上传功能以角度2上传视频文件,并在前端的播放器中显示视频 - To upload a video file in angular 2 using ng2 file upload and display the video in a player in the front end 如何从前端下载文件-Java HTML - How to download a file from Front End - Java HTML 在Node中下载PDF文件并重定向到前端React - Download a PDF file in Node and redirect into front end React 在前端加载一堆角度指令和其他js文件 - loading a bunch of angular directives and other js files on the front end 使用Web API和angular JS将数据下载为Excel文件 - Download data as Excel file using web api and angular JS 如何在使用Angular JS发送参数的同时下载文件 - How to download a file while sending parameters using Angular JS
 
粤ICP备18138465号  © 2020-2024 STACKOOM.COM