Sign Up

Sign Up to our social questions and Answers Engine to ask questions, answer people’s questions, and connect with other people.

Have an account? Sign In

Have an account? Sign In Now

Sign In

Login to our social questions & Answers Engine to ask questions answer people’s questions & connect with other people.

Sign Up Here

Forgot Password?

Don't have account, Sign Up Here

Forgot Password

Lost your password? Please enter your email address. You will receive a link and will create a new password via email.

Have an account? Sign In Now

You must login to ask a question.

Forgot Password?

Need An Account, Sign Up Here

Please briefly explain why you feel this question should be reported.

Please briefly explain why you feel this answer should be reported.

Please briefly explain why you feel this user should be reported.

Sign InSign Up

The Archive Base

The Archive Base Logo The Archive Base Logo

The Archive Base Navigation

  • Home
  • SEARCH
  • About Us
  • Blog
  • Contact Us
Search
Ask A Question

Mobile menu

Close
Ask a Question
  • Home
  • Add group
  • Groups page
  • Feed
  • User Profile
  • Communities
  • Questions
    • New Questions
    • Trending Questions
    • Must read Questions
    • Hot Questions
  • Polls
  • Tags
  • Badges
  • Buy Points
  • Users
  • Help
  • Buy Theme
  • SEARCH
Home/ Questions/Q 8077329
In Process

The Archive Base Latest Questions

Editorial Team
  • 0
Editorial Team
Asked: June 5, 20262026-06-05T15:27:02+00:00 2026-06-05T15:27:02+00:00

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

  • 0

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){
   // setting mime this way is for example only
   var mime = 'application/vnd.openxmlformats-officedocument.wordprocessingml.document';
   
   jQuery.ajax({ url      : 'get_file.pl',
                 data     : {filename:filename}, 
                 success  : function(data){
                               var win = window.open('','title');
                               win.document.open(mime);
                               win.document.write(data);
                               win.document.close();
                            }
               });
}

jQuery('#imgID').dblclick(function(){ 
   getFile('someFile.docx');
});

I’m doing this off the top of my head, but I think the above would work for text files, but not binary. Is there a plugin that does this properly? The ideal would be to open the file in the browser (or application), rather than download, but I doubt that is a dream. If the file must be downloaded with the save/open dialog, that’s fine.


Edit:

One piece of information that I forgot to mention is that I’d like this to be a POST request. This is partly why I was looking at AJAX to begin with. I’ve seen workarounds that have created forms/iframes to do something similar, but I was looking for a better handler of the returned info.

  • 1 1 Answer
  • 0 Views
  • 0 Followers
  • 0
Share
  • Facebook
  • Report

Leave an answer
Cancel reply

You must login to add an answer.

Forgot Password?

Need An Account, Sign Up Here

1 Answer

  • Voted
  • Oldest
  • Recent
  • Random
  1. Editorial Team
    Editorial Team
    2026-06-05T15:27:03+00:00Added an answer on June 5, 2026 at 3:27 pm

    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
    • Reply
    • Share
      Share
      • Share on Facebook
      • Share on Twitter
      • Share on LinkedIn
      • Share on WhatsApp
      • Report

Sidebar

Related Questions

I'm sending a file using the response << file.newInputStream (similar to this answer here
I need to split this/similar string to get the VALUE <a href=javascript:void(0); id=def_ name=color
This is very similar to: Vim auto commands: writing a read-only file? Except that
I usually loop through lines in a file using the following code: open my
This question is very similar to Loading a file into a vector ; however,
I found this similar question here , but this is really old. Was it
I have been reading through this similar question and find myself less than satisfied
int main() { int p; scanf(%d,&p); fun() { int arr[p]; // isn't this similar
This is similar to Linux GREP/SED [xargs] find pattern and line number in files
This is similar to a question I asked a while ago, however this didn't

Explore

  • Home
  • Add group
  • Groups page
  • Communities
  • Questions
    • New Questions
    • Trending Questions
    • Must read Questions
    • Hot Questions
  • Polls
  • Tags
  • Badges
  • Users
  • Help
  • SEARCH

Footer

© 2021 The Archive Base. All Rights Reserved
With Love by The Archive Base

Insert/edit link

Enter the destination URL

Or link to existing content

    No search term specified. Showing recent items. Search or use up and down arrow keys to select an item.