2

I am trying to post canvas to the server.

First I am converting the canvas to the form data which I found here:

Service:

appRoot.service('FormService', function () {
    return {
        dataURItoBlob: function (dataURI) {
            // convert base64/URLEncoded data component to raw binary data held in a string
            var byteString;
            if (dataURI.split(',')[0].indexOf('base64') >= 0)
                byteString = atob(dataURI.split(',')[1]);
            else
                byteString = unescape(dataURI.split(',')[1]);

            // separate out the mime component
            var mimeString = dataURI.split(',')[0].split(':')[1].split(';')[0];

            // write the bytes of the string to a typed array
            var ia = new Uint8Array(byteString.length);
            for (var i = 0; i < byteString.length; i++) {
                ia[i] = byteString.charCodeAt(i);
            }

            return new Blob([ia], { type: mimeString });
        }
    };
});


var dataURL = myCanvas.toDataURL('image/jpeg', 0.5);
var blob = FormService.dataURItoBlob(dataURL);

Then I am trying to post using angularjs which I found here :

                var fd = new FormData();
                fd.append("canvasImage", blob);
                var uploadURL = "/Quiz/ImageToPDF";

                FileUploadResource.save(uploadURL, fd).then(function (response) {

                });

Factory:

appRoot.factory('FileUploadResource', function ($http, $q, $timeout) {
    return {
        response: {},
        save: function (uploadUrl, fileData) {
            var file = fileData;
            var deferred = $q.defer();
                $http.post(uploadUrl, file, {
                    withCredentials: true,
                    headers: { 'Content-Type': undefined },
                    transformRequest: angular.identity
                }).
                    success(function (data, status, headers, config) {
                        deferred.resolve(data);
                    }).
                    error(function (data, status, headers, config) {
                    });
            return deferred.promise;
        }
    }
});

C# Controller Action:

[HttpPost, ActionName("ImageToPDF")]
public string ImageUploader(HttpPostedFileBase file)
{
  //file is null
 ...
}

Following are the values of variables in console:

DataURI: enter image description here

blob: enter image description here

fd: enter image description here

But I am getting null file on my server. What I am missing?

Community
  • 1
  • 1
Saurabh Palatkar
  • 3,242
  • 9
  • 48
  • 107
  • "But I am getting null file on my server" - does that mean that no data were uploaded at all? Did you set breakpoint in the server method which accepts this file? Is it send in the right format? (can server understand the data...) – David Votrubec Jun 23 '15 at 07:28
  • Yah, when the debugger hit my server action method, the input parameter "file" which is of type "HttpPostedFileBase" is null. Now as per my understanding there can be two reasons for this: 1) Either no data was sent from the client 2) or server rejected the sent data because of incorrect format. – Saurabh Palatkar Jun 23 '15 at 07:41

0 Answers0