duan201444 2017-02-13 10:07
浏览 101
已采纳

如何使用AngularJS服务和Laravel Controller将文件保存在文件夹中

I am not getting how can I send file form service to laravel controller and store it in a folder.

Here is view and controller (Angularjs Front end) View

<html>

 <head>
  <script src = "https://ajax.googleapis.com/ajax/libs/angularjs/1.3.14/angular.min.js"></script>
 </head>

  <body ng-app = "myApp">

  <div ng-controller = "myCtrl">
    <form  enctype="multipart/form-data">
     <input type = "file" file-model = "myFile"/>
     <button ng-click = "uploadFile()">upload me</button>
     </form>
  </div>

 ** controller**
  <script>
     var myApp = angular.module('myApp', []);

     myApp.directive('fileModel', ['$parse', function ($parse) {
        return {
           restrict: 'A',
           link: function(scope, element, attrs) {
              var model = $parse(attrs.fileModel);
              var modelSetter = model.assign;

              element.bind('change', function(){
                 scope.$apply(function(){
                    modelSetter(scope, element[0].files[0]);
                 });
              });
           }
        };
     }]);

     myApp.service('fileUpload', ['$http', function ($http) {
        this.uploadFileToUrl = function(file, uploadUrl){
           var fd = {'file' : file}
           var file = {
            'file' : file
           }
            console.log(uploadUrl, fd)
            $http.post("http://myipaddress/practice/upload/blog/public/index.php/uploadavtar",uploadUrl, fd)
            .then(function(response) {
            console.log(response.data)
            });
        }
     }]);

     myApp.controller('myCtrl', ['$scope', 'fileUpload', function($scope, fileUpload){
        $scope.uploadFile = function(){
           var file = $scope.myFile;

           console.log('file is ' );
           console.dir(file);

           var uploadUrl = "/fileUpload";
           fileUpload.uploadFileToUrl(file, uploadUrl);
        };
     }]);

  </script>

Laravel Controller

<?php

 namespace App\Http\Controllers;

 use Illuminate\Http\Request;

 use App\Http\Requests; 
 use Illuminate\Support\Facades\Input;

class UsersController extends Controller
{
public function uploadavtar(Request $request){


     $extension = Input::file('file')->getClientOriginalExtension();
     $filename = rand(11111111, 99999999). '.' . $extension;
     Input::file('file')->move(
       '192.168.2.68/practice/upload/public/files/uploads/', $filename
     );

      $fullPath = 'myipaddress/practice/upload' . $filename;

     return  $fullPath ;

}
 }

I checcked the above larave controller through postman its working fine only problem is its not storing the file. And if I do same from submit button its showing error of

'Call to a member function getClientOriginalExtension() on a non-object
'

How to solve this both issues. 1. Sending file form service to laravel controller 2. storing it in defined path.

  • 写回答

1条回答 默认 最新

  • dongpu6141 2017-02-14 07:49
    关注

    I think the error saying that there is no file in the request(POST)

        if( $request->hasFile('file'))
        {
            $extension = Input::file('file')->getClientOriginalExtension();
            $filename = rand(11111111, 99999999). '.' . $extension;
            Input::file('file')->move('192.168.2.68/practice/upload/public/files/uploads/', $filename);
            $fullPath = 'myipaddress/practice/upload' . $filename;
    
            return  $fullPath ;
        }
        else
        {
            //file from post does not exist
        }
    

    EDIT

    You got the paramaters wrong in your ajax

    myApp.service('fileUpload', ['$http', function ($http) {
            this.uploadFileToUrl = function(file, uploadUrl){
               var fd = new FormData();
               fd.append('file', file);
    
                $http.post("http://myipaddress/practice/upload/blog/public/index.php/uploadavtar", fd, {
                 transformRequest: angular.identity,
                 headers: {'Content-Type': undefined,'Process-Data': false}
             })
             .success(function(response){
                console.log(response.data);
             })
             .error(function(){
                console.log("error");
             });
            }
         }]);
    

    the first paramater is for the link, second is the form data, and last is the settings

    本回答被题主选为最佳回答 , 对您是否有帮助呢?
    评论

报告相同问题?

悬赏问题

  • ¥15 素材场景中光线烘焙后灯光失效
  • ¥15 请教一下各位,为什么我这个没有实现模拟点击
  • ¥15 执行 virtuoso 命令后,界面没有,cadence 启动不起来
  • ¥50 comfyui下连接animatediff节点生成视频质量非常差的原因
  • ¥20 有关区间dp的问题求解
  • ¥15 多电路系统共用电源的串扰问题
  • ¥15 slam rangenet++配置
  • ¥15 有没有研究水声通信方面的帮我改俩matlab代码
  • ¥15 ubuntu子系统密码忘记
  • ¥15 保护模式-系统加载-段寄存器