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.
Thanks in advance Please take a look at PHP Base64 Examples. There are several examples how to convert Base64 strings to files. Yes, there are some Java Base64 Examples . I got this base64 from encode file image jpg android studio, I use Base64OutputStream but i can't view the right output in here, can anyone tell me what should I do?
To get a valid Base64 string, remove the first and last character from your string. Hello, I have the result of capturing a pdf417 code and I need to decode it without having to generate a file.
In any case, you can always convert Base64 to binary and download the result as file, regardless of its MIME type. If you are looking for the reverse process, check File to Base64.
First of all, it checks all the characters in the input data. Correct base64 data consists only of ASCII characters that include: lowercase and uppercase English letters ("a-z" and "A-Z"), regular digits ("0-9"), symbols "+" and "/", as well as the padding symbol "=" (which may only appear at the end of the string).
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.
If you love us? You can donate to us via Paypal or buy me a coffee so we can maintain and grow! Thank you!
Donate Us With