dreljie602951 2017-01-04 14:48
浏览 75

$ _FILES包含上传的文件,而Laravel请求则不包含

I am using Laravel as an API and AngularJS as the front-end to interact with it. I am trying to upload a file using ng-file-upload but I am facing a weird issue.

The global variable $_FILES only contains the uploaded while $request->file('img') is empty so the custom request validation fails as it can not see the uploaded file.

I have found couple of answers stating to add use Illuminate\Http\UploadedFile; to the controller so I can access the uploaded file but I can't so far. I have checked the code having enctype="multipart/form-data" in the form but still nothing works

How can I access the file and at the same time the validation rules are applied correctly?

Angular Form:

<div class="small-6 medium-3 columns small-centered">
    <form name="products.storeForm" ng-submit="products.store()" enctype="multipart/form-data">
        <label>Name</label>
        <input type="text" ng-model="products.product.name" name="name" required>
        <label>Image</label>
        <img ng-src="{{image_source}}">
        <input type="file" name="img" onchange="angular.element(this).scope().setFile(this)" ngf-pattern="image/*" accept="image/*" ng-model="products.product.img">
        <button type="submit" class="button">Submit</button>
    </form>
</div>

Angular Service:

service.store = function(product){
    console.log(product.img)
    return Upload.upload({
        url: url,
        headers: { 'Content-Type' : 'application/json' },
        method: 'POST',
        sendFieldAs: 'form',
        fields: {
            name: product.name
        },
        file: product.img,
        fileFormDataName: 'image'
    })
};
  • 写回答

2条回答 默认 最新

  • doujiku1028 2017-01-04 19:30
    关注

    Don't have the means to test this, but I the fact that you are passing the image with the image key would be my starting point (on your Angular upload service):

    fileFormDataName: 'image'
    

    But you are trying to fetch it using the img key in Laravel:

    $request->file('img')
    
    评论

报告相同问题?

悬赏问题

  • ¥100 嵌入式系统基于PIC16F882和热敏电阻的数字温度计
  • ¥15 cmd cl 0x000007b
  • ¥20 BAPI_PR_CHANGE how to add account assignment information for service line
  • ¥500 火焰左右视图、视差(基于双目相机)
  • ¥100 set_link_state
  • ¥15 虚幻5 UE美术毛发渲染
  • ¥15 CVRP 图论 物流运输优化
  • ¥15 Tableau online 嵌入ppt失败
  • ¥100 支付宝网页转账系统不识别账号
  • ¥15 基于单片机的靶位控制系统