The input is not a valid Base-64 string as it contains a non-base 64 character, more than two padding characters, or an illegal character among the padding characters.

I was trying to upload a file in client side and sending back the data from the client to Web API controller by angular service .

Step 1 :
I was doing it by. (It was bot the complete code )

 function saveFile(file, cb) {
    var readerObj = new FileReader();
    readerObj .readAsDataURL(file);
    readerObj .onload = function () {
        savedFileDataAsBase64Format(readerObj .result, file.name,cb);
    };
    readerObj .onerror = function (error) {
        alert("Error in uploading the file.");
    };

Step 2 :

    This is my Web api controller action method.
       [HttpPost]
        [Route("api/FileInput/SaveBase64FileData/{fileName}")]
        public void SaveBase64FileData(string fileName, [FromBody]string fileData)
        {
            byte[] bytes = Convert.FromBase64String(fileData);
           // Write code what ever you want to do
        }

So from the first line of the actions method I was getting the mentioned error.

So did the following things to fix the issue.

 fileData = fileData.Replace("data:;base64,", "").Replace("data:application/octet-stream;base64,","");

It worked for me .

Please share this one if it really helped you.

Comments

Popular posts from this blog

Enable CORS in Web Api.

How to create services in Angular 2

Unable to load one or more breakpoints in Visual studio.