简体   繁体   English

通过点击与jquery的链接来下载单词docs,pdf,xls等

[英]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. 我想在下载框中打开文档(即pdf,excel,word doc等)。 for some reason the excel files work correctly but none of the others do. 出于某种原因,excel文件正常工作,但其他文件都没有。 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). 我想我需要使用javascript setHeader(我相信jquery没有这样做的核心功能,但如果我错了就纠正我)。 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? 当我这样做时,我得到一个错误“response.setHeader响应未定。有没有人有任何想法?

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. 您无法使用JavaScript更改服务器的响应,并且您无法控制浏览器决定在选择此/新窗口之外打开新链接的方式。 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. 由服务器来决定为您的请求返回什么 - 即它可以针对所有请求或任何其他类型的响应使用相同的JPG图像进行响应。

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 ). 如果您的服务器是ASP.Net one( http://support.microsoft.com/kb/260519 ),则代码正确。

声明:本站的技术帖子网页,遵循CC BY-SA 4.0协议,如果您需要转载,请注明本站网址或者原文地址。任何问题请咨询:yoyou2525@163.com.

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