我正在使用Php和Angular构建项目,我可以将文件上传到本地数据库,我可以检索文件信息。我不知道如何检索文件本身并将其下载到我的电脑。有人可以帮忙吗? 在数据库中 - 列/ type = id-int,name-varchar,mime-longblob,size-biginet,data-mediumblob,created-datetime
用于检索文件详细信息的Php:
header('Content-Type: text/html; charset=utf-8');
$connection = mysqli_connect('localhost', 'root', '', 'hamatkin');
mysqli_query($connection, "SET character_set_client = utf8");
mysqli_query($connection, "SET character_set_connection = utf8");
mysqli_query($connection, "SET character_set_results = utf8");
if (!$connection) {
die("couldnt connect".mysqli_error);
}
$query = "SELECT id, name, created FROM file";
$queryResult = $connection->query($query);
$queryResult2 = array();
if ($queryResult === false) {
die($connection->error);
}
if ($queryResult->num_rows > 0) {
while ($row = $queryResult->fetch_assoc()) {
$queryResult2[] = $row;
}
}
$queryResult3 = json_encode($queryResult2);
echo json_encode($queryResult2);
控制器:
“use strict”;
angular.module('dataSystem').controller('allPriceOffersCtrl', function($scope,$route,$location,$http) {
$http({method:'GET', url:'api/customers-tab/get-all-priceOffers.php/'})
.then(function(response) {
var arr = response.data;
$scope.files = arr;
})
// This will log you the error code and trace, if there is an error.
.catch(function(err) {
console.log('err', err)
});
});
HTML:
<div class="table-responsive">
<table class="customer-list table table-striped">
<thead>
<tr>
<!-- <th>#</th> -->
<th class="Column-Header"> מספר קובץ הצעת מחיר</th>
<th class="Column-Header">שם הקובץ</th>
<th class="Column-Header">תאריך</th>
</tr>
</thead>
<tbody>
<tr ng-repeat="x in files ">
<!-- <td>{{$index + 1}}</td> -->
<td>{{ x.id}}</td>
<td>{{ x.name}}</td>
<td> {{ x.created}} </td>
</tr>
</tbody>
</table>
</div>
答案 0 :(得分:1)
更好的解决方案是将文件上传到服务器并添加.htaccess以限制访问,将文件重命名为序列号,这样两个具有相同名称的文件永远不会覆盖并存储包含filename.extension(相对文件)的文件路径来自网站的路径),serialNo。作为文件的id和实名(带扩展名)。根据需要填充路径并在下载前重命名。
此Example是相关的