Take the 2-minute tour ×

I want to write a file-download page for customers to download files from my company servers.

And I don't want to expose the real file link to users, so I have two pages : filelist.php for offering a link to click and posting the file name as parameter to the other page, named download.php , accepting the request and transferring file stream to customers.

The key part of download.php is like this:

$fp = fopen($real_file_path,'r');
$file_size = $temp_res['ef_file_bytes'];
Header("Accept-Ranges: bytes");
Header("Accept-Length: ".$file_size);
Header("Content-Disposition: attachement;filename=".$file_name);
echo fread($fp,$file_size);

Now I use an onclick() function in filelist.php ,when the file link is clicked ,the function following is called:

function downloadfile( ef_name )
        var url_str='download.php';
        url_str = encodeURI(url_str);
        url_str,{fnm:ef_name },

But I can only show the file's content in page rather than popping up a dialog-window to save it.

If I use to test , it works well.

The the 'data' in JS code above is exactly the whole content of the file I want to down load. I think the question is in the function I used in $.post(); Is there any function in javascript can open a new page with post parameter ?

I'm on windows , apache ,php,mysql.Thanks!

share|improve this question
You should consider looking for existing Q/A with the same topic. –  ibtarek Sep 19 '12 at 11:03
@PradeepSanjaya, the OP wants it to be through POST method because that's how he receives data on the other side. Your links only show how to do it through GET. –  SparK Sep 19 '12 at 14:04
add comment

1 Answer

You can't use $.post but you can create a hidden form with method="POST" and submit it. Optionally a hidden iframe to use as target to recieve the result.

share|improve this answer
yeah , thanks, it does work when I use a form with 'POST' method.It will be much easier . So I counldn't find a way using JS code to send a post request and pop up a dialog window to save files ? I just feel strange -_-| –  ChiangRay Sep 20 '12 at 0:59
you can use JS to form.submit()... unfortunately. To send a post and recieve binary data you would have to open a socket and assemble the request yourself, the $.ajax() method won't help you and neighter will the XMLHttpRequest object... –  SparK Sep 20 '12 at 12:40
add comment

Your Answer


