Question

After googling, I found that Magic numbers can be used to identify the content type of a file.

In my program, I would like to validate the file content type on server side.

My client side code :

<form action="/Home/Index" method="post" enctype="multipart/form-data">
    <input type="file" id="inputFile" value="" onchange="readFileContent(this)" />
    <input type="submit" value="Submit" />
</form>

function readFileContent(input) {
        if (input.files && input.files[0]) {

            reader = new FileReader();
            reader.onload = function (e) {

                var xhr = new XMLHttpRequest();
                xhr.open('POST', '/Home/CheckFileType', true);
                xhr.setRequestHeader("Content-Type", "multipart/form-data");
                xhr.setRequestHeader('X-File-Name', input.files[0].name);
                xhr.setRequestHeader('X-File-Type', input.files[0].type);
                xhr.setRequestHeader('X-File-Size', input.files[0].size);
                xhr.send(input.files[0]);
                xhr.onreadystatechange = function () {
                    if (xhr.readyState == 4 && xhr.status == 200) {
                        alert(xhr.responseText);
                    }
                }

            };
            reader.readAsText(input.files[0]);
        }
    }

And this is my server side code :

[HttpPost]
        public JsonResult CheckFileType()
        {
            string fileType = Request.Headers["X-File-Type"];
            byte[] buffer = new byte[Request.InputStream.Length];
            Request.InputStream.Read(buffer, 0, Convert.ToInt32(Request.InputStream.Length));

            object result = new { status = "finished" };
            return Json(result, JsonRequestBehavior.AllowGet);
        }

What is the magic number for a plain-text or .txt file

Était-ce utile?

La solution

Magic numbers in the context discussed here are often used to indicate what kind of data is in a binary file. A program that parses a file can look at the magic number and then know what to do with the rest of the file. For example, the magic number for all Java .class files (not source files) is 0xCAFEBABE. At runtime when the classloader loads a class it will look at those first 4 bytes and if they aren't 0xCAFEBABE, the class loader will not treat the file as a valid Java class file. If you were defining your own file type for some software you were writing or expected others to write, you could define your own magic number or numbers. When software created files of your type it would be that software's responsibility to write the appropriate magic number in the file. The software that reads the files could use that magic number to help decide what to do.

Magic numbers do not make sense for plain text files. If you write a magic number to the file, it would no longer be a plain text file. It would be a file that follows your format which might be a magic number followed by a bunch of plain text. If that is what you want, then do it. I don't know what your app is doing but conceivably that might make sense as long as you know the files will always be read and written by your own software (or other software which is compliant with your magic number expectations).

I hope that helps.

Licencié sous: CC-BY-SA avec attribution
Non affilié à StackOverflow
scroll top