开发者

asp.net mvc c# checking file type and file size

开发者 https://www.devze.com 2023-01-20 18:32 出处:网络
What is the best way to check the file type and size in asp.net mv开发者_开发问答c c# controllers?What is wrong with System.IO.FileInfo [1]

What is the best way to check the file type and size in asp.net mv开发者_开发问答c c# controllers?


What is wrong with System.IO.FileInfo [1]

what do you mean by 'type', extension or mime type. extension is obviously on the path and mime type mapping I think is in the registry. Size is length

[1] http://msdn.microsoft.com/en-us/library/system.io.fileinfo.aspx


To Calculate Size of file following code will be used in controller:

var fileSize = objFile.size; //size in kb
fileSize = fileSize / 1048576; //size in mb

and for checking file type you can do in Javascript

var _validFileExtensions = [".jpg", ".jpeg", ".bmp", ".gif", ".png"];    
function Validate(oForm) {
    var arrInputs = oForm.getElementsByTagName("input");
    for (var i = 0; i < arrInputs.length; i++) {
        var oInput = arrInputs[i];
        if (oInput.type == "file") {
            var sFileName = oInput.value;
            if (sFileName.length > 0) {
                var blnValid = false;
                for (var j = 0; j < _validFileExtensions.length; j++) {
                    var sCurExtension = _validFileExtensions[j];
                    if (sFileName.substr(sFileName.length - sCurExtension.length, sCurExtension.length).toLowerCase() == sCurExtension.toLowerCase()) {
                        blnValid = true;
                        break;
                    }
                }

                if (!blnValid) {
                    alert("Sorry, " + sFileName + " is invalid, allowed extensions are: " + _validFileExtensions.join(", "));
                    return false;
                }
            }
        }
    }

    return true;
}


Like this you can get the extension of a file:

string extension = Path.GetExtension(upload.FileName);

This will include a leading ..

Note that the you should not assume that the extension is correct.

NOTE

Determining type from byte[] is not very straightforward. You'll have to assume using MagicStrings, using unmanaged code, or do investigative work like iteratively consume as various types until it doesn't fail, etc. If this is an internal app, consequences of mis-typed file uploads can be mitigated on the consuming end, and the threat of malicious use is exceedingly low, relying on the extension is probably reasonable enough compared to expense of doing the rest.

I am not sure may be you can use MIME

using System.Web.MimeMapping class that is part of the BCL in .NET Framework 4.5:

string mimeType = MimeMapping.GetMimeMapping(fileName);


you can use Myrmec to identify the file type, this library use the file byte head. this library avaliable on nuget "Myrmec",and this is the repo, myrmec also support mime type,you can try it. the code will like this :

// create a sniffer instance.
Sniffer sniffer = new Sniffer();

// populate with mata data.
sniffer.Populate(FileTypes.CommonFileTypes);

// get file head byte, may be 20 bytes enough.
byte[] fileHead = ReadFileHead();

// start match.
List<string> results = sniffer.Match(fileHead);

and get mime type :

List<string> result = sniffer.Match(head);
string mimeType = MimeTypes.GetMimeType(result.First());
0

精彩评论

暂无评论...
验证码 换一张
取 消