0
嗨,夥計們,我只需要在我的項目下載頁面上獲得一些幫助,因爲我需要一個下載頁面,可以從不同文件夾獲取文件,所有文件夾都在公共路徑中一些想法,我正在使用一個頁面就像下面的鏈接只是忽略其他按鈕。laravel 4從不同文件夾下載文件
我只是想阿賈克斯這一點,但它不工作
這是我的觀點:
@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");
}
});
});
});
我的控制器:
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);
}
我的路線:
Route::get('/downloadfile/{id}', '[email protected]');
Route::get('/download', array('uses' => '[email protected]'));
任何想法更多讚賞先謝謝了!
如果我將下載的文件是pdf文件和圖像文件,我會在數組中添加額外的代碼嗎?例如:$ headers = array( 'Content-Type:=> application/jpg', \t'Content-Type:=> application/pdf', ); –
是的,你可以按照你的要求添加到數組中。 –
謝謝你的想法,但仍然有一些錯誤,顯示文件沒有在我的公共路徑中找到,我很迷惑{$ files-> filepath}在哪裏? –