简体   繁体   English

如何使用Java脚本Ajax调用下载txt文件

[英]how to download txt file with using java script ajax call

I want to download a text file (ex:-somthing.txt). 我想下载一个文本文件(例如:-somthing.txt)。 When I use an ancor tag , the file is downloaded, but i want to download the file using an ajax call. 当我使用ancor标签时,文件已下载,但是我想使用ajax调用下载文件。

HTML code: HTML代码:

<body>
    <a href="#" id="exportViewRule">Export</a>
</body>

JavaScript code : JavaScript代码:

$("#exportViewRule").click(function(){
            $.ajax({
                url : "/download/myDir/exportFile",
                type : "GET",
                contentType : "text/plain",             
                success : function(data){
                    alert(data);
                }

        });
    });

java code: Java代码:

@Path("/myDir")
public class IdnsDataHandler {

@GET
    @Path("/exportFile")
    @Produces("text/plain")
    public Response exportFile(){
        File file=new File("/home/cerdik/Desktop/some.text");
        ResponseBuilder response=Response.ok((Object)file);
        response.header("Content-Disposition","attachment; filename=export-file.text");
        return response.build();
    }
}

When i use this code bellow (without javascript), the download works. 当我使用下面的代码(没有javascript)时,下载有效。

HTML code: HTML代码:

<body>
    <a href="./download/myDir/exportFile" id="exportViewRule">Export</a>
</body>

Thanks to all, and i found another solution for this type of downloading method, now i did not use ajax call, below shows to my success code 谢谢大家,我找到了这种下载方法的另一种解决方案,现在我没有使用ajax调用,下面显示了我的成功代码

Html: HTML:

<a id="exportView" style="cursor:pointer">Export</a>

javaScript: JavaScript的:

$("#exportView").click(function(){
            var exportId = $('#serviceRules option:selected').attr("stream");
            var TakeHref="./download/myDir/exportFile"+exportId;
            document.getElementById("exportView").setAttribute("href", TakeHref);
        });

this code running successfully in my app, thank you all. 此代码在我的应用中成功运行,谢谢大家。

I think you have to include the file ending to the url (eg .txt) 我认为您必须包含以url结尾的文件(例如.txt)

$.ajax({
   url : "/download/myDir/exportFile.txt",
   ...
})

The answer is: No you can't . 答案是: 不,你不能

AJAX requests are done within a rendered page in the browser. AJAX请求是在浏览器中呈现的页面内完成的。 They request the content of a file and that is stored inside the XMLHTTPObject . 他们请求文件的内容,该内容存储在XMLHTTPObject The response header for content-type will not make a difference and is ignored by the browser. content-type的响应标头没有任何区别,并且被浏览器忽略。

To be more precise : 更准确地说

An AJAX call is executed inside a page for which the response header is set to TEXT/HTML , that means that the content of the file is requested by AJAX. AJAX调用在页面内执行,该页面的响应标头设置为TEXT/HTML ,这意味着AJAX请求文件的内容。 The response header will not be reset by the call and therefor will not trigger a download. 响应头不会被调用重置,因此不会触发下载。

When clicking a link, the response header describing the content of the file is sent to the page by the JAVA code resulting in a response that is treated as a download by the browser. 单击链接时,描述文件内容的响应标头通过JAVA代码发送到页面,从而导致响应被浏览器视为下载。

you should use ajax call and the trick is defining dataType then the response will be what the .txt file contains. 您应该使用ajax调用,技巧是定义dataType,然后响应将是.txt文件包含的内容。

$.ajax({
        url : "some-file.txt",
        dataType: "text",
        success : function (data) {
            // set text to preferred DOM
            $("THE-DOM").html(data);
        }
    });

If you really need JS intervention to have your params changed than this code snippet should do. 如果您确实需要JS干预来更改您的参数,则此代码段应该做的事情不应该这样做。 Not a good way to do though. 虽然不是一个好方法。

 //your JS function to manipulate the url function downloadFile(){ var url = "/download/myDir/exportFile";//+your extra params $('#fake').prop({'src':url}); } 
 <a href="#" onclick="downloadFile()" id="exportViewRule">Export</a> <iframe name="fake" id="fake" style="display:none;"></iframe> 

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

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