duan201444
duan201444
2017-02-13 10:07

如何使用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 dongpu6141 4年前

    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

    点赞 评论 复制链接分享

为你推荐