1

I'm trying to write a script to download a file. But when I click "Download" nothing happens. I'm using Laravel for my project. This is the function:

public function downloadUserFile(){
        $userid = Auth::id();
        $result = $_POST['filename'];
        $query = File::where('filename', $result)->where('userid', $userid)->get();
        foreach($query as $queryResult){
            $mimeType = $queryResult->mimetype;
            $filepath = $queryResult->filePath;
            $filesize = $queryResult->filesize;
            if (file_exists($filepath)) {
                header('Content-Description: File Transfer');
                header('Content-Type: ' . $mimeType);
                header('Content-Disposition: attachment; filename="'.basename($filepath).'"');
                header('Expires: 0');
                header('Cache-Control: must-revalidate');
                header('Pragma: public');
                header('Content-Length: ' . $filesize);
                ob_clean();
                flush();
                readfile($filepath);
                exit;
            }
        }       
    }

And the ajax:

           if(key === "download") {
                var classElements = document.querySelectorAll("tr.ui-selected td.filename");
                var csrf = $('input[name=_token]').val();
                for(var x = 0;x < classElements.length;x++){
                    var result;
                    result = classElements[x].innerHTML;
                    $.ajax({
                        async: true,  
                        method: 'POST',
                        url: '../public/downloadfile',
                        data: { filename: result, "_token": csrf  }   
                    });
                };
            }

The ajax response and the PHP don't give me errors and I can't understand why nothing actually happens. What could be the problem?

Alex
  • 485
  • 6
  • 18
  • you don't do anything with the dowloaded data yet, so there can nothing happen yet. Checkout the callback-functions of $.ajax! How do you know the ajax-response is ok? But anyway, see the link I've posted! – Jeff Dec 20 '15 at 20:42
  • Possible duplicate of [Download file through an ajax call php](http://stackoverflow.com/questions/6668776/download-file-through-an-ajax-call-php) – Jeff Dec 20 '15 at 20:43
  • How should I proceed with the data? – Alex Dec 20 '15 at 20:44
  • @Alex If you are looking to download the file for the user just use the `download` attribute on a direct link to the file. More information: https://davidwalsh.name/download-attribute – Jon Koops Dec 20 '15 at 20:49
  • use https://github.com/rndme/download to download an existing url with `download("/my/file.ext");` one nice thing: it doesn't need special headers (like content-disposition) from the server... – dandavis Dec 20 '15 at 21:48
  • You cannot process download requests via Ajax. – Ozan Kurt Dec 20 '15 at 21:56
  • Laravel has a function like response()->download() check that out it will help you. – Ozan Kurt Dec 22 '15 at 03:16

0 Answers0