简体   繁体   中英

force word docs, pdf, xls, etc to download by clicking on a link with jquery

I am trying to get documents(ie. pdf, excel, word doc, etc) to open in a download box. for some reason the excel files work correctly but none of the others do. I think I need to setHeader using javascript(i believe that jquery does not have core functions that do this, but correct me if i'm wrong). here is a sample of my code.

$(function() {
    $('a.media-link').click(function(event){
        var fileName = $(this).html();
        var property_id = $("input[name=capturePropId]").val();
        //alert(fileName);
        event.preventDefault();  //stop the browser from following
        window.location.href = '../uploads/properties/'+
        property_id+'/media/'+fileName+'';
        response.setHeader("Content-Disposition", "attachment; 
        filename=\"" + fileName + "\"");
    });
});

When i do this i get an error "response.setHeader response is undefinded. does anyone have any ideas?

You need to set the headers differently for each filetype. Take a look here .

You can't change server's response with JavaScript and you can't control how browser decides to open a new link outside of chosing this/new window. It is up to server to decide what to return for your request - ie it can response with the same JPG image for all requests or any other type of response.

You need to put the code that sets headers on the server that serves the request. You have correct code if your server is ASP.Net one ( http://support.microsoft.com/kb/260519 ).

The technical post webpages of this site follow the CC BY-SA 4.0 protocol. If you need to reprint, please indicate the site URL or the original address.Any question please contact:yoyou2525@163.com.

 
粤ICP备18138465号  © 2020-2024 STACKOOM.COM