Angularjs $http post file and form data

后端 未结 8 1149
广开言路
广开言路 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 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); 
    };
    

    }

提交回复
热议问题