java - AngularJS - File Download through AJAX -


I created an angular JS program to download a file from the server, the code is followed here

HTML code

  & lt; a download = "FullListkcsv" Anji- href = "{{fullListUrl}}" type = "button" class = "btn btn- success Bitian-XS Akgik-batch export" NG-Click = "Sisbiblack (Bacakssi)" & Gt; & Lt; Span class = "glyphicon glyphicon-ok" & gt; & Lt; / Span & gt; Export as CSV & lt; / A & gt;  

AngularJS controller

  $ scope.exportCSVBulk = function () {var page = "../importExportService/exportBulkCSV/ "+ BatchExec.id; $ Http.get (page) Ksuccess (function (response) {$ scope.fullListUrl = 'data: text / CSV; charset = UTF-8' + escape (response);}); }  

What am I doing here when a user links to export CSV function as exportCSVBulk fire and that function URL value (Fullistuarel) sets but it is an Ajax request, so when a user clicks on the URL link, the response time is a little bit as a result will not be redirected to the correct URL. Is it possible to fix this problem? Or is there any alternative way to fix this?

I have encountered a similar problem for downloading such files through Ajax. PDF,. XLS, .xlsx etc.

It is a fact that we can not download files through Ajax, even if I came up with a solution that downloads files through Ajax.

You can use - A jQuery file download plugin for Ajax, rich file download feature.

server side

I'm using spring on server side

  @ RequestMapping, (value = "exportXLS", method = RequestMethod.POST, output = APP_JSON) @ResponseBody public void getCSV (last HttpServletResponse response @RequestParam (value = "empId", required = true) last string empId) IOException, exception { Last Byte [] throws CSV = ExportXLSUtil.getFileBytes (empId); // Get file bytes OutputStream output = getOutputStream (response); Response.setHeader ("content-displacement", "attachment; file name = document_" + new date time () + ".xls"); Response.setContentType (CONTENT_TYPE); Response.setContentLength (csv.length); Write (output, csv); }  

Client side

In the client side, I am using AngularJS

  $ downloadXLS = function (id) { $ FileDownload ('/ user / exportXLS', {httpMethod: "post", data: {empId: id}}). Done (function (e, feedback) {// success}} failed (function (e, response) {// failure}); }   


Comments

Popular posts from this blog

java - Can't add JTree to JPanel of a JInternalFrame -

javascript - data.match(var) not working it seems -

javascript - How can I pause a jQuery .each() loop, while waiting for user input? -