how can i check the file name and format of the image? RRS feed

  • Question

  • User-159723402 posted
    If i want to limit the file format of photo for a client that upload to sevrer/forum. How can i check the file format in C#? thanks, dark0125
    Wednesday, March 2, 2005 8:19 PM

All replies

  • User2028753447 posted
    I got this script off of a example, that i dont have on this PC.

    Basicaly what it does is gets the filename from the upload page and makes it into an array by seperating it from the dot

    'Save as same file name being posted
        'The code below resolves the file name
        '(removes path info)
        sFile = txtUpload.PostedFile.FileName
        sSplit = Split(sFile, "\")
        sFile = sSplit(Ubound(sSplit))
        ' Get Name without extension, split from dot into array
        sNameSplit = Split(sFile, ".")
        sFileName = sNameSplit(0)
        sFileNameb = sNameSplit(1)

    So SNameSplit(1) would be your extension i.e. gif, jpg etc etc

    Sorry its not in C# im still getting to grips with vb.net, should be easy to convert though.

    Wednesday, April 6, 2005 5:00 AM
  • User-95607119 posted

    Use FileInfo class from System.IO namespace to find out file extension.

    System.IO.FileInfo f = new System.IO.FileInfo(Request.Files[0].FileName);
    string sExt= f.Extension.ToLower(); // here is extension

    The other way is to use System.Drawing namespace classes with uploaded file and see what format the file has. In this case your system cannot be tricked by chaning file extension. 

    Thursday, April 7, 2005 3:45 AM