AngularJS: upload and post photo as multi-part form data and get response

前端 未结 1 559
暖寄归人
暖寄归人 2021-02-06 05:48

I want to upload an image and post it to my server which should post the image further on to some API and get its response.

I could probably use view with something like

相关标签:
1条回答
  • 2021-02-06 06:11

    I managed to accomplish this with the help of custom directive that I got here. You need a custom directive for file upload if you want to access files (ngModel wont work, see this for more info).

    Using first link:

    <form ng-submit="updateSth()" name="uploadForm" enctype="multipart/form-data">
        <input name="file" type="file" id="file" file-model="fileToUpload" />
        <input name="somethingINeed" type="submit" value="Upload" />
    </form> 
    

    you can access the uploaded file with $scope.fileToUpload now, which makes everything easier. I extracted the function from the service given on the first link and didn't use it as a service since it suited me better this way …

    0 讨论(0)
提交回复
热议问题