Welcome Guest, Not a member yet? Register   Sign In
Problem with download helper and AJAX calls. Expertise needed.
#6

[eluser]gRoberts[/eluser]
Hi there,

Sorry, the forum removed part of my response.

#1 was "window dot location dot href" but replace the " dot " with a full stop.

#2 Could work in both situations. if you are using Ajax, then really, the iframe doesn't "have" to already exist. You could create it when you get a response back from the server.

i.e.

Code:
(function()
{
  $('#download').click(function(e)
  {
   e.preventDefault();
   $.ajax(
   {
    url : 'controller/action',
    dataType : 'json',
    type : 'POST',
    data :
    {
     Image1 : 'friend.jpg',
     Image2 : 'dog.jpg'
    },
    success : function(resp)
    {
     // using the iframe method
     if($('.download-frame').length == 0)
     {
      $('body').append($('<iframe class="download-frame" src="about:blank" />').css({ width : 1, height : 1 }));
      $('.download-frame').attr('src', resp.DownloadPath);
     }

     // using the [removed].href method
     [removed].href = resp.DownloadPath;
    },
    error : function()
    {
     alert('There was a problem generating the ZIP');
    }
   });
  });
});

Note: The above sample includes BOTH #1 and #2 methods, you will need to remove one or the other, otherwise it may result in two downloads appearing.

This would mean you have to change your PHP code slightly to, instead of "force_downloading" the file, you simply use json_encode to return the filename.

i.e.

Code:
$Filename = md5(time()) . '.zip';
if(!file_exists($Filename))
{
  //create zip
}
header('content-type: application/json');
die(json_encode(array('DownloadPath' => base_url() . 'cache/' . $Filename)));

Obviously, the part where you generate the filename and do all the checking is relevant, but instead of using force_download, you simply return the DownloadPath as a response in which the Ajax call (in javascript) will receive and change the iFrame to.


Messages In This Thread
Problem with download helper and AJAX calls. Expertise needed. - by El Forum - 05-17-2012, 10:48 PM



Theme © iAndrew 2016 - Forum software by © MyBB