douzhi9635 2015-08-28 13:00
浏览 268

如何使用Angular File Upload(ng-file-upload)将图片上传到后端Laravel / PHP

I having been trying to wrap my head around ng-file-upload but without much success.

I'm trying to upload a picture using ng-file-upload to backend PHP/Laravel

I have this in my angular controller:

$scope.upload = function (file) {

    Upload.upload({
        method: 'POST',
        url: '/profile/photo/add',
        file: file,
        sendFieldsAs: 'form'
        //fileFormDataName: 'photo'
    })

    .success(function (data, status, headers, config) {
          dd(data);
          console.log('file ' + config.file.name + 'uploaded. Response: ' + data);
     })

    .error(function (data, status, headers, config) {
          dd(data);
          console.log('error status: ' + status);
     })

HTML Template for ng file upload:

<div ngf-select="upload($file, $event)"
  ngf-change="upload($file, $event)"
  ngf-pattern="'image/*'"
  ngf-capture="capture"
  ngf-drag-over-class="{accept:'dragover', reject:'dragover-err',      delay:100}"
  ngf-validate="{size: {min: 10, max: '1MB'}, width: {min: 200, max:10000},  height: {min: 200, max: 300}, duration: {min: '10s', max: '5m'}, pattern: 'image/*'}"
  ngf-keep="false"
  ngf-allow-dir="true" class="drop-box"
  ngf-drop-available="dropAvailable">Select Photo,
  <span ng-show="dropAvailable">Drop/Paste Photo</span>

I have also created a route/controller in Laravel

public function postAdd(PhotoRequest $request)
{
    //Here am just checking if file is valid but it always return false and uploads an unreadable file to /uploads/profile folder 
    if ($request->file('file')->isValid() && $request->hasFile('file')) {
        $request->file('file')->move(public_path('/uploads/profile/'));
        return json_encode($request->file('file')->isValid()); //returns false
    }else{
        return response()->json(['file not found']);
    }

 }

When examined in chrome console, the output looks like this: enter image description here

These unreadable files were uploaded

enter image description here

What am I doing wrong?

  • 写回答

0条回答 默认 最新

    报告相同问题?

    悬赏问题

    • ¥100 iOS开发关于快捷指令截屏后如何将截屏(或从截屏中提取出的文本)回传给本应用并打开指定页面
    • ¥15 unity连接Sqlserver
    • ¥15 图中这种约束条件lingo该怎么表示出来
    • ¥15 VSCode里的Prettier如何实现等式赋值后的对齐效果?
    • ¥15 流式socket文件传输答疑
    • ¥20 keepalive配置业务服务双机单活的方法。业务服务一定是要双机单活的方式
    • ¥50 关于多次提交POST数据后,无法获取到POST数据参数的问题
    • ¥15 win10,这种情况怎么办
    • ¥15 如何在配置使用Prettier的VSCode中通过Better Align插件来对齐等式?(相关搜索:格式化)
    • ¥100 在连接内网VPN时,如何同时保持互联网连接