0

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("Content-type: application/octet-stream");
Header("Accept-Ranges: bytes");
Header("Accept-Length: ".$file_size);
Header("Content-Disposition: attachement;filename=".$file_name);
echo fread($fp,$file_size);
fclose($fp);
exit;

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);
        $.post(
        url_str,{fnm:ef_name },
        function(data){
            window.open().document.write(data);
          }
          ); 
    }

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!

ChiangRay
  • 11
  • 1
  • [How to download a file by jquery.ajax](http://stackoverflow.com/questions/4545311/how-to-download-a-file-by-jquery-ajax) – Pradeep Sanjaya Sep 19 '12 at 10:41
  • [Ajax File Download using Jquery, PHP](http://stackoverflow.com/questions/3599670/ajax-file-download-using-jquery-php) – Pradeep Sanjaya Sep 19 '12 at 10:42
  • 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

1 Answers1

0

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.

SparK
  • 5,181
  • 2
  • 23
  • 32
  • 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 00: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