[removed] How to open a returned file via AJAX

后端 未结 1 1108
有刺的猬
有刺的猬 2021-02-11 02:00

This is similar to: How to open a file using JavaScript?

Goal: to retrieve/open a file on an image\'s double click

function getFile(filename){
   // sett         


        
相关标签:
1条回答
  • 2021-02-11 02:15

    Seems to me there's no reason to do this via AJAX. Just open the new window to get_file.pl?filename=... and let the browser handle it. If the user has a plugin capable of handling the Content-Type sent by get_file.pl, the file will display; otherwise, it should download like any other file.

    function getFile(filename) {
       window.open('get_file.pl?filename=' + filename,'title');
    }
    
    jQuery('#imgID').dblclick(function() { 
       getFile('someFile.docx');
    });
    

    Edit: If you want to POST to your script, you can do it with some <form> hackery:

    function getFile(filename) {
        var win = 'w' + Math.floor(Math.random() * 10000000000000);
        window.open('', win,'width=250,height=100');
        var f = $('<form></form>')
                .attr({target: win, method:'post', action: 'get_file.pl'})
                .appendTo(document.body);
    
        var i = $('<input>')
                .attr({type:'hidden',name:'filename',value:filename})
                .appendTo(f);
    
        f[0].submit();
        f.remove();
    }
    

    Of course, this is somewhat silly since it is impossible to hide your data from "prying eyes" with developer tools. If your filename really is sensitive, issue access tokens to the client, and look up the data in your sever script.

    0 讨论(0)
提交回复
热议问题