简体   繁体   English

从base64检查文​​件类型?

[英]Checking file type from base64?

I have a WCF REST Service with the following OperationContract that saves files on the disk:我有一个 WCF REST 服务,其中包含以下 OperationContract 将文件保存在磁盘上:

[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)文件通过javascript发送 - 使用HTML File API => 二进制数据 => base-64 编码的 ASCII 字符串(=fileContent 在操作合同中收到)

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.我知道Checking MIME Type from a base64 string on the Code Review Stack Exchange 但我不确定这是否是最好的方法。 Also, I have tested uploading several .txt files and each one has different first 5 chars.另外,我测试了上传几个 .txt 文件,每个文件的前 5 个字符都不同。

I am looking for a code snippet that would include checking for several common file types.我正在寻找一个代码片段,其中包括检查几种常见的文件类型。

Check this link here:在此处检查此链接:

https://web.archive.org/web/20170331115315/http://codeanalyse.com/2016/10/02/extracting-file-extension-base64-string/ https://web.archive.org/web/20170331115315/http://codeanalysis.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;
 }
}

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只需按照浏览器的方式进行操作 - 通过 mime 类型:在 javascript 中,通过 HTML 文件 API (evt.dataTransfer.files[0].type) 检查文件类型,然后将其作为 JSON 消息的一部分发送到服务器

Or do it the way windows does it - by the file name extension.或者按照 Windows 的方式进行操作 - 通过文件扩展名。

声明:本站的技术帖子网页,遵循CC BY-SA 4.0协议,如果您需要转载,请注明本站网址或者原文地址。任何问题请咨询:yoyou2525@163.com.

 
粤ICP备18138465号  © 2020-2024 STACKOOM.COM