doubushi0031 2014-02-25 20:24
浏览 53

AngularJS&Laravel:上传文件并抓取服务器

I'm using the angular-file-upload directive, to upload a file to my server, via a POST request, however I'm getting confused.

I have my input box setup: <input type="file" ng-model="image['file']" ng-file-select="onFileSelect($file)" >

I assign the file to the $scope.upload on select:

$scope.onFileSelect = function($file) {

    $scope.upload = $file;
};

Now, when I press my "upload" button I want to send some data ($scope.image) to the server, but also my uploaded file so I can get it validated.

I created a Collection service for this, as follows:

    ...
    validateUpload : function( data, upload ) {
        return $http({
            method: 'POST',
            url: '/url/to/upload',
            headers: { 'Content-Type' : 'application/json' },
            data: data,
            file: upload
        });
    },
    ...

Okay, so now in my controller I can try to upload the data:

Collection.validateUpload( $scope.image, $scope.upload )
    .success(function(data) {
        console.log(data);
    });

In my Laravel Controller, I can access the $scope.image data quite easily:

return Input::all();

= Object {file: "C:\fakepath\myimage.png", title: "Title", source: "A Source"} 

However, I wish to access my file. According to the Laravel docs I should be able to access this with:

Input::file('file');

However, it doesn't seem to be grabbing anything at all. For example, running ->getSize() on this complains about trying to access something which isn't an object.

Anyone got any idea how I get that file?

EDIT: Seems after a bit more playing, when I console.log($file); when selected, its undefined, hm.

  • 写回答

2条回答 默认 最新

  • donglei1973 2014-02-25 22:37
    关注

    Try this:

    $scope.onFileSelect = function($file) {
        $scope.upload = $file[0];
    };
    
    评论

报告相同问题?

悬赏问题

  • ¥20 java在应用程序里获取不到扬声器设备
  • ¥15 echarts动画效果的问题,请帮我添加一个动画。不要机器人回答。
  • ¥60 许可证msc licensing软件报错显示已有相同版本软件,但是下一步显示无法读取日志目录。
  • ¥15 Attention is all you need 的代码运行
  • ¥15 一个服务器已经有一个系统了如果用usb再装一个系统,原来的系统会被覆盖掉吗
  • ¥15 使用esm_msa1_t12_100M_UR50S蛋白质语言模型进行零样本预测时,终端显示出了sequence handled的进度条,但是并不出结果就自动终止回到命令提示行了是怎么回事:
  • ¥15 前置放大电路与功率放大电路相连放大倍数出现问题
  • ¥30 关于<main>标签页面跳转的问题
  • ¥80 部署运行web自动化项目
  • ¥15 腾讯云如何建立同一个项目中物模型之间的联系