开发者

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

开发者 https://www.devze.com 2023-02-17 09:58 出处:网络
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 javas

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 - i.e. 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).

0

精彩评论

暂无评论...
验证码 换一张
取 消

关注公众号