Tuesday 11 June 2013

Restricting File upload box type

<!-- Match all image files (image/*) -->
<input type="file" accept="image/*">

<!-- Match all video files (video/*) -->
<input type="file" accept="video/*">

<!-- Match all audio files (audio/*) -->
<input type="file" accept="audio/*">

<!-- Match all image files (image/*) and files with the extension ".someext" -->
<input type="file" accept=".someext,image/*">
<!-- See note below -->

<!-- Match all image files (image/*) and video files (video/*) -->
<input type="file" accept="image/*,video/*">
<!-- See note below -->


The accept attribute may be specified to provide user agents with a hint of what file types will be accepted.
If specified, the attribute must consist of a set of comma-separated tokens, each of which must be anASCII case-insensitive match for one of the following:

The string audio/*

  • Indicates that sound files are accepted.

The string video/*

  • Indicates that video files are accepted.

The string image/*

  • Indicates that image files are accepted.

valid MIME type with no parameters

  • Indicates that files of the specified type are accepted.

A string whose first character is a U+002E FULL STOP character (.)

  • Indicates that files with the specified file extension are accepted.

Tuesday 12 February 2013

Hashing

using System.Security.Cryptography;
private string GetHash(string text, string passwordFormat)
    {
        HashAlgorithm _algorithm;
        switch (passwordFormat.ToUpper())
        {
            case "MD5":
                _algorithm = MD5.Create();
                break;
            case "SHA1":
                _algorithm = SHA1.Create();
                break;
            case "SHA256":
                _algorithm = SHA256.Create();
                break;
            case "SHA512":
                _algorithm = SHA512.Create();
                break;
            default:
                throw new ArgumentException("Invalid password format.", "passwordFormat");
        }
        byte[] bytes = Encoding.UTF8.GetBytes(text);
        byte[] hash = _algorithm.ComputeHash(bytes);
        string hashString = string.Empty;
        foreach (byte x in hash)
        {
            hashString += String.Format("{0:x2}", x);
        }
        return hashString.ToLowerInvariant();
    }