php – 请求是空laravel ajax多个图像

php – 请求是空laravel ajax多个图像,第1张

概述所以我有一些功能正常的代码,但我需要添加多个图像上传到它.我使用了 this plugin.现在我的HTML看起来像这样: <form class="form-horizontal full-width pull-left m-t-20" name="myForm" ng-if="showForm"> <div class="form-group"> <label for="N 所以我有一些功能正常的代码,但我需要添加多个图像上传到它.我使用了 this plugin.现在我的HTML看起来像这样:
<form  name="myForm" ng-if="showForm">    <div >      <label for="Naam" >Naam</label>      <div >        <input type="text"  ID="Naam" ng-model="addForm.name" placeholder="Naam">      </div>    </div>    <div >      <div >        Afbeeldingen        <input type="file" ngf-select ng-model="files" ngf-multiple="true" accept="image/*" />        Drop afbeeldingen: <div ngf-drop ng-model="files" >Drop</div>      </div>      <div >        <div ng-repeat="file in files" >          <img ng-show="myForm.file.$valID" ngf-thumbnail="file" ><br /> <button ng-click="file = null"  ng-show="file">Verwijder</button>        </div>      </div>    </div>    <div >      <span  ng-show="files.progress >= 0">        <div  ng-bind="files.progress + '%'"></div>      </span>      <div >        <button type="submit" ng-click="addStore(files)" >Opslaan</button>      </div>    </div></form>

这是我的JavaScript代码:

.controller('StoresController',['$scope','$http','handlerService','Upload','$timeout',function($scope,$http,handlerService,Upload,$timeout) {  $scope.model = {};  $scope.model.stores = stores;  $scope.showForm = true;  $scope.addForm = {};  $scope.addForm.name = '';  $scope.addStore = function(files) {    $scope.showForm = true;    files.upload = Upload.upload({     method: 'POST',url: 'http://localhost:8000/stores/add/',data: {store : $scope.addForm,files: files},});    files.upload.then(function (res) {      $timeout(function () {        handlerService.isValID(res.data);        if(res.data.isValID == true) {            $scope.showForm = false;            $scope.addForm = {};        }      });    },function (response) {      if (response.status > 0)        $scope.errorMsg = response.status + ': ' + response.data;      },function (evt) {      // Math.min is to fix IE which reports 200% sometimes      files.progress = Math.min(100,parseInt(100.0 * evt.loaded / evt.total));    });  }}])

而后端部分:

namespace App\http\Controllers;use Illuminate\http\Request;use App\http\Requests;use DB;class StoresController extends Controller{    public function add(Request $request) {      $files = array();      foreach($_fileS as $file) {        $filename = $file['name'];        $files[] = $file['name'];        $destination = '../public/assets/img/stores/' . $filename;        move_uploaded_file($file['tmp_name'],$destination );      }      $store = $request->store;      $store['images'] = Json_encode($files);      $isValID = false;      if(isset($store['name']) && $store['name'] != '') {        DB::table('stores')->insert(            $store        );        $isValID = true;        $message = 'store is added.';      } else {        $message = Json_encode($request->all());        $isValID = false;      }      $result = [        'isValID' => $isValID,'message' => $message      ];      return Json_encode($result);    }}

但整个请求都是空的.没有文件上传部分,我可以毫无问题地添加商店.当我检查我的AJAX请求时,我还可以看到有效负载与存储和图像一起发送.我究竟做错了什么?

我需要提供resumeSizeUrl或resumeSize,以检查文件的进度.我这样做后,我的文件发送没有问题.

后端部分也不对.为了上传文件我使用了laravel的Request类.这是当前的后端代码:

public function add(Request $request) {  $isValID = true;  DB::beginTransaction();  $store = $request->store;  if(isset($store['name']) && $store['name'] != '') {    $ID = DB::table('stores')->insertGetID(        $store    );  } else {    $message = 'Naam mag nIEt leeg zijn';    $isValID = false;  }  if($isValID === true) {    // getting all of the post data    $files = $request->file('files');    // Making counting of uploaded images    $path = 'stores'.'/'.$ID;      $file_count = count($files);      // start count how many uploaded      $uploadcount = 0;      foreach($files as $file) {        $rules = array('file' => 'mimes:png,gif,jpeg'); //'required|txt,pdf,doc'        $valIDator = ValIDator::make(array('file'=> $file),$rules);        if($valIDator->passes()) {          $destinationPath = 'img/'.$path.'/';          $filename = $file->getClIEntOriginalname();          $upload_success = $file->move($destinationPath,$filename);          $uploadcount ++;        } else {          $message = 'Alleen afbeeldingen zijn toegestaan';          $isValID = false;        }      }      if($isValID == true) {        if($uploadcount != $file_count) {          $message = 'NIEt alle bestanden zijn geupload';          $isValID = false;        }      }  }  if($isValID == true) {    DB::commit();    $message = 'Store Toegevoegd!';  }  if($isValID == false) {    DB::rollBack();  }  $result = [    'isValID' => $isValID,'message' => $message  ];  return Json_encode($result);}
总结

以上是内存溢出为你收集整理的php – 请求是空laravel ajax多个图像全部内容,希望文章能够帮你解决php – 请求是空laravel ajax多个图像所遇到的程序开发问题。

如果觉得内存溢出网站内容还不错,欢迎将内存溢出网站推荐给程序员好友。

欢迎分享,转载请注明来源:内存溢出

原文地址: https://outofmemory.cn/langs/1263496.html

(0)
打赏 微信扫一扫 微信扫一扫 支付宝扫一扫 支付宝扫一扫
上一篇 2022-06-08
下一篇 2022-06-08

发表评论

登录后才能评论

评论列表(0条)

保存