IT정보사전

[.Net] 파일관련 Framework(확장자, 파일명, 이미지사이즈, 첨부파일) 본문

웹 프로그래밍

[.Net] 파일관련 Framework(확장자, 파일명, 이미지사이즈, 첨부파일)

작은나무0530 2018. 12. 7. 21:43
728x90
반응형

파일등록시 확장자만 가져오기, 파일명만 가져오기, 이미지 사이즈 가져오기에 대한 샘플입니다.

//FileUpload Check
public bool FileUpCheck(string FileName)
{
    string[] strArray = "exe,bat,com, reg,dll,cgi,asp,aspx,cs,php,jsp,java,sql,vbs,htm,html,css,js,xml,xsd,xsl,htc,class".Split(new char[] {','});
    FileName = FileName.ToLower();
    string str = FileExe(FileName);

    for (int i = 0i <strArray.Lengthi++)
    {
        if (str == strArray[i])
            return false;
    }
    
    return true;
}

//파일명을 제외한 확장자명
public string FileExe(string FileName)
{
    string[] strArray = FileName.Split(new char[] { '.' });
    if (strArray.Length <= 1)
        return "";

    return strArray[strArray.Length - 1];
}

//확장자를 제외한 파일명
public static string returnFileName(string FileName)
{
    string retVal = "";
    if (!FileName.Equals("") &&FileName.LastIndexOf('.') >0)
        retVal = FileName.Substring(0, FileName.LastIndexOf('.'));

    return retVal;
}

//이미지 사이즈 가져오기
public static string returnImageSize(string ImageURL)
{
    string retVal = string.Empty;
    string path = string.Empty;

    path = BaseConst.strABSOLUTEPATH_FDS;
    if (path.EndsWith(@"\") == false)
        path = path + @"\";

    string FilePath = path + ImageURL;
    Bitmap Imagesize = new Bitmap(FilePath, false);
    int imgWidth = Imagesize.Width;
    int imgHeight = Imagesize.Height;

    /*float percent;
    if (imgWidth >MaxWidth)
    {
        imgWidth = MaxWidth;
        percent = (float)Imagesize.Width / (float)MaxWidth;
        imgHeight = (int)(((float)Imagesize.Height) / percent);
    }
    else if (imgHeight >MaxHeight)
    {
        imgHeight = MaxHeight;
        percent = (float)Imagesize.Height / (float)MaxHeight;
        imgWidth = (int)(((float)Imagesize.Width) / percent);
    }
    */

    retVal = imgWidth.ToString() + "," + imgHeight.ToString();
    return retVal;
}

728x90
반응형
그리드형
Comments