/// <summary>
/// 上传图片文件
/// </summary>
/// <param name="upFile">控件名称</param>
/// <param name="path">文件路径</param>
/// <param name="message">返回信息</param>
/// <returns>是否成功</returns>
public static bool upLode(FileUpload upFile, string path, out string message)
{
Boolean fileOk = false;
if (upFile.HasFile)
{
//取得文件的扩展名,并转换成小写
string fileExtension = System.IO.Path.GetExtension(upFile.FileName).ToLower();
//限定图片格式
string imageType = ConfigurationManager.AppSettings["ImageType"];
string[] allowExtension = imageType.Split(',');
//对上传的文件的类型进行一个个匹对
string imageName = "";
for (int i = 0; i < allowExtension.Length; i++)
{
if (fileExtension == allowExtension[i])
{
imageName = DateTime.Now.ToString("yyyyMMddHHmmss").ToString() + allowExtension[i];
fileOk = true;
break;
}
}
//判断文件类型是否符合
if (fileOk)
{
//对上传文件的大小进行检测,限定文件最大不超过300kb
if (upFile.PostedFile.ContentLength <= int.Parse(ConfigurationManager.AppSettings["ImageSizeLimit"]))
{
upFile.PostedFile.SaveAs(path + imageName);
message = imageName;
fileOk = true;
}
else
{
message = "文件大小超出300kb";
fileOk = false;
}
}
else
{
message = "要上传的文件类型不对!";
fileOk = false;
}
}
else
{
message = "请选择上传文件!";
fileOk = false;
}
return fileOk;
}
|