简体   繁体   中英

how to download txt file with using java script ajax call

I want to download a text file (ex:-somthing.txt). When I use an ancor tag , the file is downloaded, but i want to download the file using an ajax call.

HTML code:

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

JavaScript code :

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

        });
    });

java code:

@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.

HTML code:

<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

Html:

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

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)

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

The answer is: No you can't .

AJAX requests are done within a rendered page in the browser. They request the content of a file and that is stored inside the XMLHTTPObject . The response header for content-type will not make a difference and is ignored by the browser.

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. 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.

you should use ajax call and the trick is defining dataType then the response will be what the .txt file contains.

$.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. 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> 

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