0

I have an ajax POST request being sent to a laravel controller, this works fine. I do some work in the controller and I get a string that can vary in size from a few hundred kilobytes to hundreds of megabytes.

I want to download this string as a file and I've been trying various methods, but the problem is whenever I use return to pass the string to a different controller or a different page it returns the output to the ajax success instead. I've tried removing the success, but it still does not work.

            /*POST request*/
            $.ajax({
                type:"post",
                url:"{{ URL::route('sshDownload') }}",
                data: {
                    id:id
                    file:file,
                    status:"logfile"
                },
                success:function(data){
                    console.log(data);
                    console.log("SUCCESS");
                }
            });

In my sshDownload controller I'm trying to do something like this:

        return Response::make($output, '200', array(
            'Content-Type' => 'text/plain',
            'Content-Disposition' => 'attachment; filename="file.log'
        ));

and I've even tried:

        return Redirect::route('sshDownloadName')->with($output);

but in the end, anytime I use return it sends the data back to to the front end ajax.

How can I download the string directly from PHP instead of sending it to ajax?

Envious
  • 478
  • 1
  • 8
  • 28

1 Answers1

1

It's not 100% clear from your question what you're after -- but this sounds like a Javascript problem, and not a Laravel problem.

When you tell javascript to make an AJAX request, you're telling the browser

Hey, download this URL in the background and let me know when you're done.

An AJAX response will never trigger a browser page refresh or a file download. Instead of an AJAX request, you need to redirect the entire browser (either via document.location, window.open or something similar) to another page entirely.

Alana Storm
  • 164,128
  • 91
  • 395
  • 599
  • Yeah I figured it wasn't a laravel issue. I've been trying to stop the success in Ajax, but this doesn't work. So would I have to submit a form or something ? – Envious Aug 29 '14 at 20:37
  • @Envious Ignore the previous comment, I missed you needed POST. Yes, standard Javascript technique is creating a form and then posting it. http://stackoverflow.com/questions/133925/javascript-post-request-like-a-form-submit – Alana Storm Aug 29 '14 at 20:51