Download Image via Ajax + Laravel

0

I'm trying to make a script for Image Download, by Ajax. It can be one or several. As you can see in JS, it has a each function that looks for the selected images in the system. Hence, I call Ajax to take the Photo ID as a parameter so that I can search the database, find the address of the photo and download it.

The code returns the encoded image instead of downloading. What do I need to change? Do I need to use AJAX?

// Download de Imagens
   $("#download").click(function(){
      var ids = '';
      $("input[name='foto[]']:checked").each(function(){
         ids = $(this).val() + ',' + ids;
      });

      ids = ids.substr(0,ids.length-1);
      $.ajax({
         url: urlBase + '/portfolio/download/',
         cache: false,
         type: "POST",
         responseType: 'blob',
         data: {ids:ids}
      });
   });
# Download de Imagens 
public function anyDownload(){
  $ids = Input::get('ids');
  $imagem = GaleriaProjeto::find($ids);
  $headers = array(
    'Content-Type: image/jpg',
  );
  return Response::download(URL::to('/img/portfolio/'.$imagem->imagem, $imagem->imagem, $headers));
}
    
asked by anonymous 30.04.2015 / 16:41

1 answer

0

Solved, and I even did a scheme to zip the images.

 // Download de Imagens
   $("#download").click(function(){
      var ids = '';
      $("input[name='foto[]']:checked").each(function(){
         ids = $(this).val() + ',' + ids;
      });

      ids = ids.substr(0,ids.length-1);
      $.ajax({
         url: urlBase + '/portfolio/download/',
         cache: false,
         type: "POST",
         data: {ids:ids},
         success: function(result){
            if(result)
               window.location.href = urlBase + '/portfolio/baixar/'+result;
         }
      });
   });

php:

        # Download de Imagens 
        public function anyDownload(){
            $ids        = Input::get('ids');
            $imagem     = GaleriaProjeto::whereIn('id', explode(',', $ids))->get();

            $zipper = new \Chumper\Zipper\Zipper;
            $zipper->make('img/portfolio/imagens.zip');

            foreach($imagem as $arr){
                $zipper->add('img/portfolio/'.$arr->imagem);
            }
            return 'imagens.zip';
        }

        # Baixar Imagens
        public function getBaixar($slug){
            return Response::download('img/portfolio/'.$slug);
        }
    
30.04.2015 / 19:30