Laravel 使用 AngularJS 仅存储多个文件上传文件中的一个文件
Laravel storing only one file from multiple file upload file using AngularJS
早上好还是晚上好(取决于您何时阅读此主题)。
我使用 angular 和 laravel 编写了这段代码来上传多个文件,
但是我从请求中得到的只是一个文件对象,所有其他的都被释放了,因此 laravel 控制器不会在 foreach 和 return undefined.
中循环
请给我反馈
HTML
<input ng-file-model = "files" value="Seleziona file" type="file"
name = "file[]" class="btn btn-info btn-s" multiple />
控制器(AngularJS)
$scope.upload = function(){
var obj = {};
obj.pratica = $scope.pra.id;
obj.cliente = $scope.cliente.id;
obj.tipoDoc = $scope.getTipoDoc;
obj.file = $scope.files;
dati.uploadFile(obj)
.success(function(result){
$rootScope.stampaGritter(result.message, "success");
})
.error(function(result){
$rootScope.stampaGritter(result.message, "error");
});
};
app.directive('ngFileModel', ['$parse', function ($parse) {
return {
restrict: 'A',
link: function (scope, element, attrs) {
var model = $parse(attrs.ngFileModel);
var isMultiple = attrs.multiple;
var modelSetter = model.assign;
element.bind('change', function () {
angular.forEach(element[0].files, function (item) {
scope.files.push(item);
});
scope.$apply(function () {
if (isMultiple) {
modelSetter(scope, scope.files);
} else {
modelSetter(scope, scope.files[0]);
}
});
});
}
};
}]);
uploadFile: function(obj) {
var fd = new FormData();
angular.forEach(obj.file, function(item) {
fd.append('file', item);
});
fd.append('tipo_doc', obj.tipoDoc);
fd.append('cliente', obj.cliente);
fd.append('pratica', obj.pratica);
$http.post(
"api/storeFile",
fd,
{
transformRequest: angular.identity,
headers: {'Content-Type': undefined
}
})
},
控制器(Laravel)
public function storeFile(Request $request) {
/*$file = $request->file('file');
$filename = $file->getClientOriginalName();
$guid = $this->getGUID();
$file->move(storage_path()."\app\file", $guid);
$response = [
"file" => $filename,
"GUID" => $guid
];
$file = new Documenti($response);
$file->save();*/
$cli = $request->input('cliente');
$pra = $request->input('pratica');
$tipoDoc = $request->input('tipo_doc');
$files = $request->file('file');
dd($files);
foreach($files as $file) {
$guid = $this->getGUID();
$file->move(storage_path()."\app\file", $guid);
$response = [
'cliente_id' => $cli,
'pratica_id' => $pra,
'tipo_id' => $tipoDoc,
'file' => $file->getClientOriginalName(),
'GUID' => $guid
];
$file = new Documenti($response);
$file->save();
}
return response()->json($response);
}
当 dd($files) 打印时
请求负载:
------WebKitFormBoundary1yzFmBAy2rTa73eV
Content-Disposition: form-data; name="file"; filename="autorizzazioni2.CSV"
Content-Type: application/octet-stream
------WebKitFormBoundary1yzFmBAy2rTa73eV
Content-Disposition: form-data; name="file"; filename="bustapaga.pdf"
Content-Type: application/pdf
------WebKitFormBoundary1yzFmBAy2rTa73eV
Content-Disposition: form-data; name="tipo_doc"
7
------WebKitFormBoundary1yzFmBAy2rTa73eV
Content-Disposition: form-data; name="cliente"
2
------WebKitFormBoundary1yzFmBAy2rTa73eV
Content-Disposition: form-data; name="pratica"
2
------WebKitFormBoundary1yzFmBAy2rTa73eV--
Laravel 有 store
函数使用这个
foreach ($request->file as $file) {
$filename = $file->store('folder');
}
这里folder
是storage/app/folder
你需要像这样改变FormData
,你必须在FormData
中添加file[]
数组
for (let i = 0; i < obj.file.length; i++) {
let f = obj.file[i];
fd.append('file[]', f);
}
早上好还是晚上好(取决于您何时阅读此主题)。
我使用 angular 和 laravel 编写了这段代码来上传多个文件, 但是我从请求中得到的只是一个文件对象,所有其他的都被释放了,因此 laravel 控制器不会在 foreach 和 return undefined.
中循环请给我反馈
HTML
<input ng-file-model = "files" value="Seleziona file" type="file"
name = "file[]" class="btn btn-info btn-s" multiple />
控制器(AngularJS)
$scope.upload = function(){
var obj = {};
obj.pratica = $scope.pra.id;
obj.cliente = $scope.cliente.id;
obj.tipoDoc = $scope.getTipoDoc;
obj.file = $scope.files;
dati.uploadFile(obj)
.success(function(result){
$rootScope.stampaGritter(result.message, "success");
})
.error(function(result){
$rootScope.stampaGritter(result.message, "error");
});
};
app.directive('ngFileModel', ['$parse', function ($parse) {
return {
restrict: 'A',
link: function (scope, element, attrs) {
var model = $parse(attrs.ngFileModel);
var isMultiple = attrs.multiple;
var modelSetter = model.assign;
element.bind('change', function () {
angular.forEach(element[0].files, function (item) {
scope.files.push(item);
});
scope.$apply(function () {
if (isMultiple) {
modelSetter(scope, scope.files);
} else {
modelSetter(scope, scope.files[0]);
}
});
});
}
};
}]);
uploadFile: function(obj) {
var fd = new FormData();
angular.forEach(obj.file, function(item) {
fd.append('file', item);
});
fd.append('tipo_doc', obj.tipoDoc);
fd.append('cliente', obj.cliente);
fd.append('pratica', obj.pratica);
$http.post(
"api/storeFile",
fd,
{
transformRequest: angular.identity,
headers: {'Content-Type': undefined
}
})
},
控制器(Laravel)
public function storeFile(Request $request) {
/*$file = $request->file('file');
$filename = $file->getClientOriginalName();
$guid = $this->getGUID();
$file->move(storage_path()."\app\file", $guid);
$response = [
"file" => $filename,
"GUID" => $guid
];
$file = new Documenti($response);
$file->save();*/
$cli = $request->input('cliente');
$pra = $request->input('pratica');
$tipoDoc = $request->input('tipo_doc');
$files = $request->file('file');
dd($files);
foreach($files as $file) {
$guid = $this->getGUID();
$file->move(storage_path()."\app\file", $guid);
$response = [
'cliente_id' => $cli,
'pratica_id' => $pra,
'tipo_id' => $tipoDoc,
'file' => $file->getClientOriginalName(),
'GUID' => $guid
];
$file = new Documenti($response);
$file->save();
}
return response()->json($response);
}
当 dd($files) 打印时
请求负载:
------WebKitFormBoundary1yzFmBAy2rTa73eV
Content-Disposition: form-data; name="file"; filename="autorizzazioni2.CSV"
Content-Type: application/octet-stream
------WebKitFormBoundary1yzFmBAy2rTa73eV
Content-Disposition: form-data; name="file"; filename="bustapaga.pdf"
Content-Type: application/pdf
------WebKitFormBoundary1yzFmBAy2rTa73eV
Content-Disposition: form-data; name="tipo_doc"
7
------WebKitFormBoundary1yzFmBAy2rTa73eV
Content-Disposition: form-data; name="cliente"
2
------WebKitFormBoundary1yzFmBAy2rTa73eV
Content-Disposition: form-data; name="pratica"
2
------WebKitFormBoundary1yzFmBAy2rTa73eV--
Laravel 有 store
函数使用这个
foreach ($request->file as $file) {
$filename = $file->store('folder');
}
这里folder
是storage/app/folder
你需要像这样改变FormData
,你必须在FormData
file[]
数组
for (let i = 0; i < obj.file.length; i++) {
let f = obj.file[i];
fd.append('file[]', f);
}