A day with .Net

My day to day experince in .net

Valid DICOM File checking in JavaScript

Posted by vivekcek on May 9, 2014

Today i will show you, How we can check wheather a file is a DICOM file or not through JavaScript.
One year ago i wrote about how we can achieve this through c#. Please refer this “Valid Dicom File c#

Normally the extension for a DICOM file is ‘.dcm’. But this extension never guarantee the file is a valid DICOM file.

DICOM is a binary file, in a DICOM file first 128(0-127) bytes is unused. the next 4 bytes(128-131) contain letters ‘D’,’I’,’C’,’M’.

So our logic is to ready all bytes from 128-131 using some binary reader and equate with ‘D’,’I’,’C’,’M’

128=>’D’
129=>’I’
130=>’C’
131=>’M’

header

<input type="file" id="dcmUpload" />

<script type="text/javascript">

    //binding event handler to input,This work in chrome and firefox. Use attachevent in IE
    document.getElementById("dcmUpload").addEventListener("change", handleInput, false);

    function handleInput(e) {
        if (e.target.files.length == 0) {
            return;
        }

        var file = e.target.files[0];
        var reader = new FileReader();
        reader.readAsArrayBuffer(file);

        //Fired after sucessful file read, Please read documenation for FileReader
        reader.onload = function (evt) {
            if (evt.target.readyState === FileReader.DONE) {

                var array = new Uint8Array(evt.target.result);
                var s = "";
                var start = 128, end = 132;
                for (var i = start; i < end; ++i) {
                    s += String.fromCharCode(array[i]);
                }

                if (s == "DICM") {

                    alert("DICM found not a valid dicom file")
                }

                else {

                    alert("DICM not found");
                }
            }
        };

    }

</script>
Advertisements

Leave a Reply

Fill in your details below or click an icon to log in:

WordPress.com Logo

You are commenting using your WordPress.com account. Log Out / Change )

Twitter picture

You are commenting using your Twitter account. Log Out / Change )

Facebook photo

You are commenting using your Facebook account. Log Out / Change )

Google+ photo

You are commenting using your Google+ account. Log Out / Change )

Connecting to %s