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

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

报告相同问题?

悬赏问题

  • ¥20 机器学习能否像多层线性模型一样处理嵌套数据
  • ¥20 西门子S7-Graph,S7-300,梯形图
  • ¥50 用易语言http 访问不了网页
  • ¥50 safari浏览器fetch提交数据后数据丢失问题
  • ¥15 matlab不知道怎么改,求解答!!
  • ¥15 永磁直线电机的电流环pi调不出来
  • ¥15 用stata实现聚类的代码
  • ¥15 请问paddlehub能支持移动端开发吗?在Android studio上该如何部署?
  • ¥20 docker里部署springboot项目,访问不到扬声器
  • ¥15 netty整合springboot之后自动重连失效