maztt
maztt

Reputation: 12294

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

What is the best way to check the file type and size in asp.net mvc c# controllers?

Upvotes: 0

Views: 2689

Answers (4)

RocketRobin
RocketRobin

Reputation: 221

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

Upvotes: 0

user6448640
user6448640

Reputation:

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

Upvotes: 0

Bhaumik Shah
Bhaumik Shah

Reputation: 382

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;
}

Upvotes: 0

Adam Straughan
Adam Straughan

Reputation: 2848

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

Upvotes: 3

Related Questions