9

I have a WCF REST Service with the following OperationContract that saves files on the disk:

[OperationContract]
[WebInvoke(UriTemplate = "FileSave", Method = "POST", RequestFormat = WebMessageFormat.Json, ResponseFormat = WebMessageFormat.Json, BodyStyle = WebMessageBodyStyle.WrappedRequest)]
ResponseHandler FileSave(string fileName, string fileContent);

Files are sent through javascript - using HTML File API => binary data => base-64 encoded ASCII string (=fileContent is recieved in the operation contract)

I want to check the file type before saving the file on the disk. I am aware of Checking MIME Type from a base64 string on the Code Review Stack Exchange but I am not sure if it is the best way to go. Also, I have tested uploading several .txt files and each one has different first 5 chars.

I am looking for a code snippet that would include checking for several common file types.

2 Answers 2

36

Check this link here:

https://web.archive.org/web/20170331115315/http://codeanalyse.com/2016/10/02/extracting-file-extension-base64-string/

This "would include checking for several common file types"

/// <summary>
/// To demonstrate extraction of file extension from base64 string.
/// </summary>
/// <param name="base64String">base64 string.</param>
/// <returns>Henceforth file extension from string.</returns>
public static string GetFileExtension(string base64String)
{
var data = base64String.Substring(0, 5);

switch (data.ToUpper())
 {
     case "IVBOR":
        return "png";
     case "/9J/4":
         return "jpg";
     case "AAAAF":
         return "mp4";
     case "JVBER":
         return "pdf";
     case "AAABA":
         return "ico";
     case "UMFYI":
         return "rar";
     case "E1XYD":
         return "rtf";
     case "U1PKC":
        return "txt";
     case "MQOWM":
     case "77U/M":
        return "srt";
     default:
        return string.Empty;
 }
}

Note: If you are using a web browser upload process, the string may have data:image/png;base64, at the start. This should be stripped out first. This part of the string cannot be trusted for web uploads because libraries will add this based on the extension, not on the actual file type. I.e. an excel file named accounts.xlsx.pdf will be marked as a PDF, not xlsx. Looking at the content as shown above is a more trustable way to evaluate the file.

3
  • 5
    Link is no longer valid.
    – mgarde
    Oct 25, 2018 at 8:47
  • Here is a link I found that might help with this: codereview.stackexchange.com/questions/29301/…
    – Edd
    Nov 7, 2019 at 15:54
  • The base64 string of txt file has many different header substring(0,5), you are checking against only one "U1PKC", it fails on changing the txt files. so far i have identified fours substrings for text file. please let me know any ways to identify txt file
    – venkatesh
    Jan 2 at 10:35
0

Trying to figure out the file type by checking the file content is always prone to errors - you don't know all possible file types, file headers change etc...

Just do it the either the way browsers do it - by the mime type: In javascript, check the file type via the HTML File API (evt.dataTransfer.files[0].type), then send that as part of your JSON message to the server

Or do it the way windows does it - by the file name extension.

1
  • Thank you. I agree that the solution I have is prone to errors. I can have several javascript clients (not only my consumer application). Therefore I don't trust what mime type or extension the user sends.
    – ralucarer
    Jun 4, 2014 at 16:09

Your Answer

Reminder: Answers generated by Artificial Intelligence tools are not allowed on Stack Overflow. Learn more

By clicking “Post Your Answer”, you agree to our terms of service and acknowledge that you have read and understand our privacy policy and code of conduct.

Not the answer you're looking for? Browse other questions tagged or ask your own question.