Search code examples
c#file-uploaddicom

Unable to upload file to Orthanc server from C# application


I am trying to upload a DICOM file to local Orthanc server but I get the error that:

Unknown Tag & Data (2d2d,6664) larger (1647393075) than remaining bytes (76) in file, premature end of stream
E0424 16:02:20.786940 FromDcmtkBridge.cpp:1925] Cannot parse an invalid DICOM file (size: 84 bytes)

I have wrote the following code to upload the file on the server:

DicomFile dicomfile = new DicomFile(dataset);
dicomfile.Save("dicomfile.dcm");

////finally uploading the file to Orthanc
String dcm = File.ReadAllText("./test9signedLimited.dcm");

HttpClient client = new HttpClient();

client.DefaultRequestHeaders.Add("ContentType", "multipart/form-data");
HttpContent content = new MultipartFormDataContent();

content.Headers.ContentType= new MediaTypeHeaderValue("multipart/form-data");

var response = client.PostAsync("http://localhost.:18888/instances", content).Result;

response.EnsureSuccessStatusCode();
var r = response.Content.ReadAsStringAsync().Result;

Note I have tried to upload the same file using POSTMAN and it was successfully uploaded there.

Thanks for your help in advance.


Solution

  • Not knowing anything about C#, but I see two problems in the code at the moment.

    1. You read in the DICOM file from disk, but I don't see any code to actually attach the DICOM data to the POST request. It seems, that You are posting an empty request with only headers.
    2. You use File.ReadAllText to read the DICOM file from disk, which returns a String. This does not seem right, since DICOM is a binary format and converting to it to a string is more than likely to break it. You more likely need a byte array with the DICOM data to attach to the POST request.