嗨大家我只需要在我的项目中的下载页面上获得一些帮助,因为我需要一个下载页面,从不同的文件夹中获取文件所有文件夹都在公共路径中你对我有一些想法我正在使用一个页面就像下面的链接一样,只是忽略另一个按钮。
我刚尝试了ajax,但它不起作用
我的观点:
@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}', 'FileController@dloadFile');
Route::get('/download', array('uses' => 'FileController@getDownload'));
任何想法都非常感谢提前感谢!
答案 0 :(得分:0)
无需使用Ajax进行下载。有我的代码下载产品image.in查看文件有下载链接。
<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>
在routes.php中
Route::get('productCRUD/{product}/download', 'ProductCRUDController@download');
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);
}
使用此代码,当您单击下载链接时,您将获得下载文件夹中的文件。