简体   繁体   中英

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

在 asp.net mvc c# 控制器中检查文件类型和大小的最佳方法是什么?

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());

The technical post webpages of this site follow the CC BY-SA 4.0 protocol. If you need to reprint, please indicate the site URL or the original address.Any question please contact:yoyou2525@163.com.

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