Angularjs $http post file and form data

后端 未结 8 1126
广开言路
广开言路 2020-11-28 22:14

I have the below request in python

import requests, json, io

cookie = {}
payload = {\"Name\":\"abc\"}
url = \"/test\"
file = \"out/test.json\"

fi = {\'file         


        
相关标签:
8条回答
  • 2020-11-28 22:51

    I recently wrote a directive that supports native multiple file uploads. The solution I've created relies on a service to fill the gap you've identified with the $http service. I've also included a directive, which provides an easy API for your angular module to use to post the files and data.

    Example usage:

    <lvl-file-upload
        auto-upload='false'
        choose-file-button-text='Choose files'
        upload-file-button-text='Upload files'
        upload-url='http://localhost:3000/files'
        max-files='10'
        max-file-size-mb='5'
        get-additional-data='getData(files)'
        on-done='done(files, data)'
        on-progress='progress(percentDone)'
        on-error='error(files, type, msg)'/>
    

    You can find the code on github, and the documentation on my blog

    It would be up to you to process the files in your web framework, but the solution I've created provides the angular interface to getting the data to your server. The angular code you need to write is to respond to the upload events

    angular
        .module('app', ['lvl.directives.fileupload'])
        .controller('ctl', ['$scope', function($scope) {
            $scope.done = function(files,data} { /*do something when the upload completes*/ };
            $scope.progress = function(percentDone) { /*do something when progress is reported*/ };
            $scope.error = function(file, type, msg) { /*do something if an error occurs*/ };
            $scope.getAdditionalData = function() { /* return additional data to be posted to the server*/ };
    
        });
    
    0 讨论(0)
  • 2020-11-28 22:55

    I was unable to get Pavel's answer working as in when posting to a Web.Api application.

    The issue appears to be with the deleting of the headers.

    headersGetter();
    delete headers['Content-Type'];
    

    In order to ensure the browsers was allowed to default the Content-Type along with the boundary parameter, I needed to set the Content-Type to undefined. Using Pavel's example the boundary was never being set resulting in a 400 HTTP exception.

    The key was to remove the code deleting the headers shown above and to set the headers content type to null manually. Thus allowing the browser to set the properties.

    headers: {'Content-Type': undefined}
    

    Here is a full example.

    $scope.Submit = form => {
                    $http({
                        method: 'POST',
                        url: 'api/FileTest',
                        headers: {'Content-Type': undefined},
                        data: {
                            FullName: $scope.FullName,
                            Email: $scope.Email,
                            File1: $scope.file
                        },
                        transformRequest: function (data, headersGetter) {
                            var formData = new FormData();
                            angular.forEach(data, function (value, key) {
                                formData.append(key, value);
                            });
                            return formData;
                        }
                    })
                    .success(function (data) {
    
                    })
                    .error(function (data, status) {
    
                    });
    
                    return false;
                }
    
    0 讨论(0)
  • Please, have a look on my implementation. You can wrap the following function into a service:

    function(file, url) {
      var fd = new FormData();
    
      fd.append('file', file);
    
      return $http.post(url, fd, {
        transformRequest: angular.identity,
        headers: { 'Content-Type': undefined }
      });
    }
    

    Please notice, that file argument is a Blob. If you have base64 version of a file - it can be easily changed to Blob like so:

    fetch(base64).then(function(response) {
      return response.blob(); 
    }).then(console.info).catch(console.error);
    
    0 讨论(0)
  • 2020-11-28 22:56

    There are other solutions you can look into http://ngmodules.org/modules/ngUpload as discussed here file uploader integration for angularjs

    0 讨论(0)
  • 2020-11-28 23:00

    In my solution, i have

    $scope.uploadVideo = function(){
        var uploadUrl = "/api/uploadEvent";
    
    
        //obj with data, that can be one input or form
        file = $scope.video;
        var fd = new FormData();
    
    
        //check file form on being
        for (var obj in file) {
            if (file[obj] || file[obj] == 0) {
                fd.append(obj, file[obj]);
            }
        }
    
        //open XHR request
        var xhr = new XMLHttpRequest();
    
    
        // $apply to rendering progress bar for any chunking update
        xhr.upload.onprogress = function(event) {
            $scope.uploadStatus = {
                loaded: event.loaded,
                total:  event.total
            };
            $scope.$apply();
        };
    
        xhr.onload = xhr.onerror = function(e) {
            if (this.status == 200 || this.status == 201) {
    
                //sucess
    
                $scope.uploadStatus = {
                    loaded: 0,
                    total:  0
                };
    
    
                //this is for my solution
                $scope.video = {};
                $scope.vm.model.push(JSON.parse(e.currentTarget.response));
                $scope.$apply();
    
            } else {
               //on else status
            }
        };
    
        xhr.open("POST", uploadUrl, true);
    
        //token for upload, thit for my solution
        xhr.setRequestHeader("Authorization", "JWT " + window.localStorage.token);
    
    
        //send
        xhr.send(fd); 
    };
    

    }

    0 讨论(0)
  • 2020-11-28 23:13

    You can also upload using HTML5. You can use this AJAX uploader.

    The JS code is basically:

      $scope.doPhotoUpload = function () {
        // ..
        var myUploader = new uploader(document.getElementById('file_upload_element_id'), options);
        myUploader.send();
        // ..
      }
    

    Which reads from an HTML input element

    <input id="file_upload_element_id" type="file" onchange="angular.element(this).scope().doPhotoUpload()">
    
    0 讨论(0)
提交回复
热议问题