Angular FIle上传自定义标题以便全部上传

时间:2016-11-21 06:22:12

标签: angularjs

我想在上传之前将自定义标头添加到文件中。以下两种方法均无效:

   $scope.uploadallman = function (qID) {
    alert(qID);

   uploader.headers = { QuestionID: qID };

    //item.headers = {
    //    MyHeader: qID,
    //};
    uploader.uploadAll();
}

1 个答案:

答案 0 :(得分:2)

也许不是最好的解决方案,但这就是我这样做的方式,而且它正在发挥作用。

function _uploadFile(resourceURL, file) {
    var deferred = $q.defer();
    var config = {};
    config.headers = {};
    config.transformRequest = angular.identity;
    config.headers['Content-Type'] = undefined;

    // config.headers['MyCustomHeaderKey'] = MyCustomHeaderValue; // Optional

    var formData = new FormData();
    formData.append('file', file);
    // formData.append('myCustomFormDataAttributeKey', myCustomFormDataAttributeValue); // Optional

    $http.post(resourceURL, formData, config)
        .success(function (response) {
            deferred.resolve(response);
        })
        .error(function (err, status) {
            deferred.reject(err);
        });

    return deferred.promise;
}

这是关于Angular中文件上传的非常好的主题:
How to POST JSON and a file to web service with Angular?