2016-11-23 2 views
0

Hallo Leute, ich brauche nur ein wenig Hilfe auf meiner Download-Seite in meinem Projekt, weil ich eine Download-Seite haben muss, die Dateien aus verschiedenen Ordnern alle Ordner im öffentlichen Pfad haben Sie haben einige ideen dafür benutze ich eine seite genau wie der untenstehende link ignoriere einfach die andere taste.Laravel 4 Datei von verschiedenen Ordnern herunterladen

Download Page

i Ajax für das gerade versucht, aber es nicht

diese meiner Meinung nach funktioniert:

@include('partials.navbar') 
<link rel="stylesheet" type="text/css" href="http://localhost:8000/assets/css/jquery.dataTables.min.css"> 
<link rel="stylesheet" type="text/css" href="http://localhost:8000/assets/css/search.css"> 
<!-- Search --> 
<div class="container"> 
    <!-- Search --> 
<div class="container"> 
<div class="row"> 
<div class="col-md-12"> 
<div class="input-group" id="adv-search"> 
    <input type="text" class="form-control" placeholder="Search file" /> 
     <div class="input-group-btn"> 
     <div class="btn-group" role="group"> 
     <div class="dropdown dropdown-lg"> 
      <button type="button" class="set-width btn-default dropdown-toggle" data-toggle="dropdown" aria-expanded="false"><span class="caret"></span></button> 
     <div class="dropdown-menu dropdown-menu-right" role="menu"> 
      <form class="form-horizontal" role="form"> 
       <div class="form-group"> 
        <label for="file">File type</label> 
        <select class="form-control"> 
        <option value="pf">Public Weather Forecast</option> 
        <option value="sf">24 Shipping Forecast</option> 
        <option value="gale">Gale Warning Forecast</option> 
        <option value="advisory">Weather Advisory</option> 
        <option value="tca">Tropical Cyclone Advisory</option> 
        <option value="swb">Severe Weather Bulletin</option> 
        <option value="iws">International Warning for shipping</option> 
        <option value="wof">Weather Outlook Forecast</option> 
        <option value="spf">Special Forecast</option> 
        <option value="sm">Surface Maps</option> 
        </select> 
       </div> 
     <div class="form-group"> 
      <label for="date">Date</label> 
      <input class="form-control" type="date" /> 
     </div> 
     <div class="form-group"> 
      <label for="file">File name</label> 
      <input class="form-control" type="text" /> 
     </div> 
      <button type="submit" class="btn btn-primary"><i class="fa fa-search"></i></button> 
      </form> 
     </div> 
     </div> 
      <button type="button" class="btn btn-primary"><i class="fa fa-search"></i></button> 
     </div> 
     </div> 
</div> 
</div> 
</div> 
</div> 
</div> 

<!--- Datatable --> 

<div class="container"> 
<div class="row">  
<div class="col-md-12"> 
    <h4>Downloads</h4> 
    <table id="mytable" class="display" cellspacing="0" width="100%"> 
    <thead> 
     <tr> 
     <th>File Name</th> 
     <th>Date Issued</th> 
     <th>File ID Number</th> 
     <th>Uploader</th> 

     <th>Action</th> 
     </tr> 
    </thead> 
    <tfoot> 
     <tr> 
     <th>File Name</th> 
     <th>Date Issued</th> 
     <th>File ID Number</th> 
     <th>Uploader</th> 

     <th>Action</th> 
     </tr> 
     </tfoot> 
     <tbody> 
     @foreach ($files as $files2) 
     <tr> 
     <td>{{ $files2->file_name }}</td> 
     <td>{{ $files2->date }}</td> 
     <td>{{ $files2->id }}</td> 
     <td>{{ $files2->username }}</td> 

     <td><a data-id="{{ $files2->id }}" href="/download" class="btn btn-primary btn-xs dload-button" ><i class="fa fa-download"></i></a> 
     <button data-id="" class="btn btn-primary btn-xs dload-button" data-title="Dload" data-toggle="modal" data-target="#dload-modal"><i class="fa fa-file-text"></i></button></td> 
     </tr> 

     @endforeach 

    </tbody> 
    </table> 
    <input type="hidden" name="id" value=""> 
    <input type="hidden" name="type" value=""> 
    <input type="hidden" name="filename" value=""> 
</div> 
</div> 
</div> 
</div> 

@include('partials.footer') 
<script type="text/javascript" src="http://localhost:8000/assets/js/jquery.dataTables.min.js"></script> 
<script type="text/javascript" src="http://localhost:8000/assets/js/dropdown.js"></script>  
<script type="text/javascript" src="http://localhost:8000/assets/js/datatable.js"></script> 
<script> 
$(function() { 
$(".dload-button").click(function(){ 
    var param = $(this).data('id'); 
    $.ajax({ 
     url: "/downloadfile/" + param, 
     success: function(msg){ 
      var dload = JSON.parse(msg)[0]; 
      console.log(dload) 
      $('#id').val(dload.id); 
      $('#type').val(dload.file_type); 
      $('#filename').val(dload.upload); 
     }, 
     error:function(){ 
      alert("failure"); 
     } 
    }); 
    }); 
}); 

mein Controller:

public function dloadFile($id) 
    { 
     $files = Files::where('id',$id) 
        ->get(); 
     return json_encode($files); 
    } 

    public function getDownload() 
    { 
     $id = Input::get('id'); 
     $files = Files::where('id',$id) 
        ->first(); 
     $ftype = $files->file_type = Input::get('type'); 
     $filename = $files->upload = Input::file('filename'); 
     $file= public_path(). "uploads/{$ftype}"; 

     $headers = array(
       'Content-Type: => application/pdf', 
      ); 

     return Response::download($file, '{$filename}', $headers); 


    } 

meine Route:

Route::get('/downloadfile/{id}', '[email protected]'); 
Route::get('/download', array('uses' => '[email protected]')); 

Jede Idee wird viel mehr geschätzt Vielen Dank im Voraus !.

Antwort

0

es gibt keine Notwendigkeit, Ajax zum Download zu verwenden. Es gibt meinen Code, um Produktbild herunterzuladen. In der Ansichtsdatei gibt es einen Downloadlink.

<a data-id="{{ $product->id }}" href="/productCRUD/{{$product->id}}/download" class="btn btn-primary btn-xs dload-button" ><i class="fa fa-download">Download</i></a> 

in routes.php

Route::get('productCRUD/{product}/download', '[email protected]'); 

in productCRUDController.php

public function download($id) 
{ 

    $files = Product::where('id',$id) 
       ->first(); 
    $ftype = $files->file_type = Input::get('type'); 
    $fullPath= public_path(). "/uploads/{$files->filePath}"; 
    $headers = array(
      'Content-Type: => application/jpg', 
     ); 
    return Response::download($fullPath,$files->filePath, $headers); 

} 

mit diesem Code, wenn Sie Download-Link klicken Sie die Datei in Ihrem Download-Ordner erhalten.

+0

Wie wäre es, wenn die Datei, die ich herunterladen werde, sowohl PDF-Datei als auch Bilddatei wäre, würde ich zusätzlichen Code im Array hinzufügen? Beispiel: $ headers = array ( 'Content-Type: => application/jpg', \t 'Inhaltstyp: => application/pdf', ); –

+0

ja Sie können in Array nach Ihren Anforderungen hinzufügen. –

+0

danke für diese Idee, aber ich habe immer noch einen Fehler, der Datei nicht in meinem öffentlichen Pfad gefunden und ich bin verwirrt in {$ files-> Dateipfad} wo ist das für? –

Verwandte Themen