gpt4 book ai didi

php - 图像未从 Angular 上传到 Laravel

转载 作者:可可西里 更新时间:2023-10-31 23:18:33 27 4
gpt4 key购买 nike

我创建了一个用于保存带有图像的项目详细信息的表单。我正在使用 laravel 5.1 和 angularJS。项目信息将成功保存,但图像未上传到服务器,图像详细信息也未保存在数据库中。我需要帮助使用 angularjs 将图像上传到 laravel

这是我的 HTML 代码:

    <div ng-controller="itemController">
<form ng-submit="addItem()">
<label>Name:</label><input type="text" name="name" value="" ng-model="newitem.name" placeholder="Item Name">
<label>Model No:</label><input type="text" name="model" value="" ng-model="newitem.model" placeholder="Model Number">
<label>Size:</label><input type="text" name="size" value="" ng-model="newitem.size" placeholder="Item Size">
<label>Colour:</label><input type="text" name="color" value="" ng-model="newitem.color" placeholder="Item Colour">
<br>
<label>Description:</label><textarea cols="30" rows="5" ng-model="newitem.description" placeholder="Description"></textarea>
<br>
<label>Photo:</label><input type="file" accept="imag/works" ngf-select="" ngf-multiple="true" class="form-control" id="img" name="img" placeholder="Image" ng-model="newitem.photo" multiple>
<br>
<button type="submit">Save</button>
</form>
<div ng-show="sendmessage">
Item Saved Successfully...........
</div>

<div>

AngularJs 代码:

app.controller('itemController',function($scope,$http,Item) {
$scope.items=[];
$scope.newitem={};
$scope.curitem = {};
$scope.sendmessage=false;

loadData();

//To Send Message to Site Admin..................

$scope.addItem=function(){
if($scope.curitem.id){
//TODO error display
$scope.newitem.$update(function () {
angular.extend($scope.curitem,$scope.curitem, $scope.newitem);
$scope.sendmessage=true;
});
}else{
$scope.newitem.$save(function (item) {
//TODO error display
$scope.items.push(item);
$scope.sendmessage=true;
});
}
//$scope.sendmessage=true;
$scope.curitem = {};
$scope.newitem = {};
$scope.newitem=new Item();
loadData();
};



function loadData(){
var items =Item.query(function () {
$scope.items = items;
$scope.newitem=new Item();
});
}
});


angular.module('ItemService',[]).factory('Item',['$resource',
function($resource){
return $resource('/api/items/:itemId',{
itemId:'@id'
},{
update:{
method:'PUT'
}
});
}
]);

ItemController.php 如下...

        <?php

namespace App\Http\Controllers\Auth;

use App\File;
use App\Item;
use Illuminate\Http\Request as Request;
use App\Http\Requests;
use App\Http\Controllers\Controller;
use Illuminate\Http\Response;
use Validator;
use Input;

class ItemController extends Controller
{
/**
* Validates given data for account
* @param array $data
* @return Validator
*/

protected function validator(array $data)
{
// Loan account is not validated

return Validator::make($data,[
'name' => 'required',
'size' => 'required|max:255'
]);

}


/**
* Store a newly created resource in storage.
*
* @param \Illuminate\Http\Request $request
* @return \Illuminate\Http\Response
*/
public function store(Request $request)
{
$validator = $this->validator($request->all());
if($validator->fails()){
return Response::json( $validator->errors()
,400);
}
$item=new Item($request->all());
if($item->save())
{
$id=$item->id;
if ($request->hasFile('photo')) {
$file = $request->file('photo');
$image=new File();
$image->name = $this->uploadImage($file);
$image->file_id=$id;
$image->save();
}
}

return Response::json(['error' => 'Server is down']
,500);
}


/**
* To Strore Images
*
*
*/

public function uploadImage($file){

$storedFileName="";
if(!empty($file)){

$extension=$file->getClientOriginalExtension();
$fileName = rand(11111,99999).'.'.$extension;
$storedFileName=base_path().'/img/works/'. $fileName;
$file->move(
base_path().'/img/works/', $fileName
);
}
return $storedFileName;
}


}

最佳答案

要通过 AngularJS 上传图片,您需要做一些额外的工作。您可以使用像 nervgh/angular-file-upload 这样的库(或其他图书馆)或自己做。更多引用 watch this tutorial by egghead

关于php - 图像未从 Angular 上传到 Laravel,我们在Stack Overflow上找到一个类似的问题: https://stackoverflow.com/questions/33030970/

27 4 0
Copyright 2021 - 2024 cfsdn All Rights Reserved 蜀ICP备2022000587号
广告合作:1813099741@qq.com 6ren.com