download a file with javascript

0

In a view HTML I have a href which has a data_id with an 'X' value and a class, when I click on the link I do ajax validating if that data (data_id) really exists, what I try to do is that in the done if I downloaded the file but ps do not do anything but I can not find the way to download it, it does not help to use <a href="download/mi_archivo.jpg" download="mi_archivo.jpg"> within HTML since I'm looking for a more elegant way to do it with JS.

This is the code with which I try to make it.

$(document).on("click", ".it-file", function()
    { 
        var txitxa = $(this).data("fileinformationstdid");
        $.ajax({
            url: 'downloadfileinformationstdid',
            type: 'POST',
            data: {'txitxa' : txitxa}
        })
        .done(function(response) {
            var answer = $.parseJSON(response);
            if(answer <= 0)
            {
                alert("Error","Este archivo no se encuentra en la base de 
                                    datos","error");
            }
            else
            {
                var url="../../download/" + answer[0].formato_filenamestd;    
                window.open(url, 'Download');
            }
        })
        .fail(function() {
            console.log("error");
        });
    
asked by JDavid 31.01.2017 в 15:18
source

2 answers

2

What you need is to make the browser go to the url you want to download ... that you get with the instruction location.href Javascript, assigning the value of the url you want to download.

Here's an example:

$(function () {
	function downloadLink(id) {
    	var ajaxOptions = {
    		url: 'http://httpbin.org/status/' + id
        };
        
        var res = $.ajax(ajaxOptions);
        
        function onAjaxDone(data) {
          
        		location.href = 'http://httpbin.org/bytes/1024';
        }
        
        function onAjaxFail() {
        	alert('Bad ID');
        }
        
        res
        	.done(onAjaxDone)
            .fail(onAjaxFail)
        ;
    }
    
	function onDownloadLinkClick(e) {
    	e.preventDefault();
        var $this = $(this);
        var id = $this.data('id');
        downloadLink(id);
    }
    
	$('.download-link').on('click', onDownloadLinkClick);
});
.download-link {
  text-decoration: none;
  border: 1px solid #ccc;
  padding: .5em;
  font-family: Helvetica;
  color: #c00;
  display: block;
  text-align: center;
  margin: 1em 0;
}
<script src="https://ajax.googleapis.com/ajax/libs/jquery/1.11.1/jquery.min.js"></script>

<a href="#" data-id="200" class="download-link">Good ID</a>
<a href="#" data-id="400" class="download-link">Bad ID</a>

Good luck and greetings!

    
answered by 31.01.2017 / 20:45
source
0

Well, I think that the " <a href="" download=""/> " is fine, if you want to leave it more beautiful simply put the <a> tag inside an image or text; without any text and so the link will not be seen but it will work in the same way, if that is the problem of using "download", it really is not a problem.

    
answered by 19.02.2018 в 17:08