Search code examples
base64dicom

Cannot get back the DICOM metadata after decode from base64 string


I am sending DICOM images to my API by encoding as base64 from the frontend, which is in Angular CLI. Also, I have Rest API to get those encoded DICOM images and decode them back before had some process with them. But after decoding the DICOM image into the memory stream, metadata of DICOM images are lost. It is appreciatable if I got a better solution. Please find my codes below.

//Angular code
var file = event.dataTransfer ? event.dataTransfer.files[i] : 
event.target.files[0];
    //var pattern = /.dcm/;
    var reader = new FileReader();
    reader.onload = this._handleReaderLoaded.bind(this);
    reader.readAsDataURL(file);


//Web API Code
[HttpPost("UploadFile/{Id}")]
public async Task<IActionResult> UploadFile(int Id, [FromBody] DICOMFiles 
dicomfiles)
{
 String base64Encoded = encodedImage;
                        string output = 
 encodedImage.Substring(encodedImage.IndexOf(',') + 1);
                        byte[] data = Convert.FromBase64String(output);
 MemoryStream stream = new MemoryStream(data);
 client.UploadFile(stream, "Projects/test_images/Test.dcm");
}

Solution

  • At last, I found a solution for this. The problem is not about decode from base64. The actual problem is with the client.UploadFile() method call.

    Before using the client.uploadfile(), we need to make sure that the memory stream object is pointing to position "0". This will allow the client.UploadFile() method to create and write all the content of the mentioned file from the start of the byte[] array. we can do this as mentioned below.

        stream.Position = 0;