【文件属性】:
文件名称:uploadFile控件上传文件,格式判断
文件大小:2KB
文件格式:TXT
更新时间:2016-11-19 08:54:50
控件上传文件
控件UpdloadFile文件上传eg:
string newfilename = file_uploadid.FileName;
string size = file_uploadid.PostedFile.ContentLength.ToString();
string type = file_uploadid.PostedFile.ContentType;
string type2 = newfilename.Substring(newfilename.LastIndexOf(".") + 1);
string path = "";
try
{
if (file_uploadid.PostedFile != null && file_uploadid.PostedFile.FileName != "")
{
string hzm = System.IO.Path.GetExtension(file_uploadid.PostedFile.FileName);//后缀名 如 .doc
string[] a = { ".txt", ".jpg", ".jpeg", ".gif", ".png", ".docx", ".doc", ".xlsx", ".xls", ".rar", ".zip", ".pdf" };//设定好了的格式
if (!a.Contains(hzm))
{
Response.Write("文件格式不正确");
}
else
{
int defaulsize = Convert.ToInt32(System.Configuration.ConfigurationManager.AppSettings["filesize"]);//取得设置的默认文件的大小
int filesize = (file_uploadid.PostedFile.ContentLength) / 1024; //取得上传的文件的大小,单位为bytes
if (filesize < defaulsize)
{
#region 对文件进行操作
newfilename = DateTime.Now.ToString("yyyyMMddHHmmssfff") + hzm;//文件的新名字 如20120711105734222.doc
path = System.Web.HttpContext.Current.Server.MapPath("~/UploadFile//");//文件保存的路径
if (!Directory.Exists(path))
{
Directory.CreateDirectory(path);
}
#endregion
}
else
{
//超过了文件的大小
Response.Write("上传的文件超过了3000M,请重新选择 ");
}
}
}
}
catch (Exception)
{
Response.Write("文件格式不正确");
}
#endregion
if (newfilename != "")
{
file_uploadid.SaveAs(path + newfilename); //保存文件
}