0

Hello Below is the code that I am using and it throws an error :

<!DOCTYPE html>
<html>
 <head>
 <title>AngularJS File Upoad Example with $http and FormData</title>
 <script src="Scripts/jquery.js"></script>
 <script src="http://xxxxxxxxxxx/angular.min.js"  type="text/javascript"></script>

<script>
    var myApp = angular.module('myApp', []);

    myApp.directive('fileModel', ['$parse', function ($parse) {
        return {
            restrict: 'A',
            link: function (scope, element, attrs) {
                var model = $parse(attrs.fileModel);
                var modelSetter = model.assign;

                element.bind('change', function () {
                    scope.$apply(function () {
                        modelSetter(scope, element[0].files[0]);
                    });
                });
            }
        };
    }]);

    myApp.service('fileUpload', ['$http', function ($http) {
        this.uploadFileToUrl = function (file, uploadUrl) {
            var fd = new FormData();
            fd.append('file', file);
            $http.post(uploadUrl, fd, {
                transformRequest: angular.identity,
                headers: { 'Content-Type': undefined }
            })
            .success(function () {
                alert("success");
            })
            .error(function () {
                alert("error");
            });
        }
    }]);

    myApp.controller('myCtrl', ['$scope', 'fileUpload', function ($scope, fileUpload) {

        $scope.uploadFile = function () {
            var file = $scope.myFile;
            alert(file);
            var uploadUrl = "http://xxxxx/listdata.svc/Documents(2)";//linke to the sharepoint document library with item id 2
            fileUpload.uploadFileToUrl(file, uploadUrl);
        };

    }]);

upload me

Janet
  • 1,391
  • 14
  • 40
  • 62
  • And what is the error? – Ola Ekdahl Mar 06 '16 at 19:02
  • I just have an alert on .error(function () { ..I am really not sure what the error is at this point. Can you help me in retrieving the error message in the error function. I am trying this code which was written already, trying to make it work for my requirement. – Janet Mar 06 '16 at 19:04
  • No 'Access-Control-Allow-Origin' header is present on the requested resource. Origin 'http://localhost:52833' is therefore not allowed access. The response had HTTP status code 400. This is the error I receive – Janet Mar 06 '16 at 20:27
  • The answer to this question should point you in the right direction, http://stackoverflow.com/questions/24134117/no-access-control-allow-origin-header-is-present-on-the-requested-resource-an – Ola Ekdahl Mar 06 '16 at 22:18
  • myApp.config(function ($httpProvider) { //Enable cross domain calls $httpProvider.defaults.useXDomain = true; delete $httpProvider.defaults.headers.common['X-Requested-With']; }); I tried this . but still it throws the same error. – Janet Mar 06 '16 at 22:38
  • I am no longer getting the above error. However, I get the error 400 : Bad request. Not sure if the I am using the url correctly to post the document to the document library in sharepoint – Janet Mar 06 '16 at 22:58
  • I just realized that you're using SharePoint 2010 and not 2013. http://stackoverflow.com/questions/7879371/best-way-to-handle-cross-domain-on-sharepoint-intranet-w-o-server-side-silverli – Ola Ekdahl Mar 06 '16 at 22:59
  • I am no longer getting the above error. However, I get the error 400 : Bad request. Not sure if the I am using the url correctly to post the document to the document library in sharepoint – – Janet Mar 06 '16 at 23:03

0 Answers0