代码改变世界

根据FileUpload控件名获取上传文件(大小)类型

2010-07-14 17:08  海蓓娜楽  阅读(1072)  评论(0编辑  收藏  举报

     /// <summary>
    /// 根据FileUpload控件名获取上传文件(大小)类型
    /// </summary>
    /// <param name="upload">FileUpload控件名</param>
    /// <returns>上传文件(大小)类型</returns>
    public string GetByteUnit(HtmlInputFile upload)
    {
        string byteUnit = null;
        if (upload.PostedFile.ContentLength < 1024)//上传文件小于1K
        {
            byteUnit = upload.PostedFile.ContentLength + "BYTE";
        }
        else if (upload.PostedFile.ContentLength >= 1024 && upload.PostedFile.ContentLength < 1048576)//上传文件大于等于1K小于1M
        {
            byteUnit = upload.PostedFile.ContentLength / 1024 + "KB";
        }
        else if (upload.PostedFile.ContentLength >= 1048576 && upload.PostedFile.ContentLength < 2097152)//上传文件大于1M小于2MB
        {
            byteUnit = upload.PostedFile.ContentLength / 1024 / 1024 + "MB";
        }
        else
        {
            byteUnit = null;           
        }
        return byteUnit;
    }