public classFileUploadService
{#region 文件上传的方法
///
///上传单个图片///
public dynamic UploadImage(HttpPostedFile file, ref stringmsg)
{
JObject obj= newJObject();try{string newFileName = string.Empty;string newFilePath = string.Empty;string newFileSize = string.Empty;//1、检查上传的文件路径是否存在//获取当前程序集的文件路径
string applicationPath =System.AppDomain.CurrentDomain.BaseDirectory.ToString();//获取程序集路径+文件夹路径
string toServerPath = applicationPath + "\\" + ConfigurationManager.AppSettings["UploadImage"];//拼接上年月文件夹( C:\\UploadFiles\\201904 )
toServerPath = toServerPath + "\\" + DateTime.Now.ToString("yyyyMM");//判断服务器文件夹路径是否存在
if (!Directory.Exists(toServerPath))
{//不存在路径,则创建
Directory.CreateDirectory(toServerPath);
}//2、检查文件的格式//指定上传文件格式类型
string[] fileExts = { ".jpg", ".jpeg", ".gif", ".png", ".bmp", ".ico"};//获取上传文件的后缀名
string fileExt =Path.GetExtension(file.FileName).ToLower();//只上传包含的文件类型
if(fileExts.Contains(fileExt))
{//获取新文件名(包含后缀名)
newFileName = Guid.NewGuid().ToString() + fileExt; //file.FileName;//拼接最终的保存文件路径(C:\UploadFiles\201904\01.jpg)
newFilePath = toServerPath + "\\" +newFileName;//检查保存是否已存在,存在不做保存
if (!System.IO.File.Exists(newFilePath))
{//3、保存上传的文件
file.SaveAs(newFilePath);
}//计算文件的大小(转成字符)
newFileSize = GetFileSize(newFilePath); //file.ContentLength; 获取文件的字节//获取相对路径
string relatePath = newFilePath.Substring(applicationPath.Length, newFilePath.Length -applicationPath.Length);//返回文件相关信息
obj.Add("Old_FileName", file.FileName);
obj.Add("FileName", newFileName);
obj.Add("FilePath", newFilePath); //文件全路径
obj.Add("FileDirect", relatePath); //相对路径
obj.Add("FileSize", newFileSize);
obj.Add("FileExt", fileExt);
}else{
msg= "上传失败,请选择扩展名为:JPG,JPEG,GIF,PNG,BMP等类型图片!";
obj= null;
}
}catch(Exception ex)
{
msg=ex.Message;
obj= null;
}returnobj;
}///
///上传单个文件///
public dynamic UploadFile(HttpPostedFile file, ref stringmsg)
{
JObject obj= newJObject();try{string newFileName = string.Empty;string newFilePath = string.Empty;string newFileSize = string.Empty;//1、检查上传的文件路径是否存在//获取当前程序集的文件路径
string applicationPath =System.AppDomain.CurrentDomain.BaseDirectory.ToString();//获取程序集路径+文件夹路径
string toServerPath = applicationPath + "\\" + ConfigurationManager.AppSettings["UploadFile"];//拼接年月文件夹
toServerPath = toServerPath + "\\" + DateTime.Now.ToString("yyyyMM");//判断服务器文件夹路径是否存在
if (!Directory.Exists(toServerPath))
{//不存在路径,则创建
Directory.CreateDirectory(toServerPath);
}//2、检查文件大小是否超过
int filebyte = file.ContentLength; //单位kb(字节)
string conSize = ConfigurationManager.AppSettings["UploadFileSize"];int conbyte = Convert.ToInt32(conSize) * 1000 * 1000;if (filebyte >conbyte)
{
msg= string.Format("上传失败,上传文件超过最大限制{0}MB!", conSize);
obj= null;
}else{//3、保存上传的文件//上传文件筛选掉图片格式
string[] fileExts = { ".jpg", ".jpeg", ".gif", ".png", ".bmp", ".ico"};//获取上传文件的后缀名
string fileExt =Path.GetExtension(file.FileName).ToLower();if(fileExts.Contains(fileExt))
{
msg= "上传失败,上传附件不包含图片格式!";
obj= null;
}else{//获取新文件名(包含后缀名)
newFileName = Guid.NewGuid().ToString() + fileExt; //file.FileName;//拼接最终的保存文件路径(C:\UploadFiles\201904\01.jpg)
newFilePath = toServerPath + "\\" +newFileName;//检查保存是否已存在,存在不做保存
if (!System.IO.File.Exists(newFilePath))
{//3、保存上传的文件
file.SaveAs(newFilePath);
}//计算文件的大小(转成字符)
newFileSize =GetFileSize(newFilePath);//获取相对路径
string relatePath = newFilePath.Substring(applicationPath.Length, newFilePath.Length -applicationPath.Length);//4、返回文件相关信息
obj.Add("Old_FileName", file.FileName);
obj.Add("FileName", newFileName);
obj.Add("FilePath", newFilePath); //文件全路径
obj.Add("FileDirect", relatePath); //相对路径
obj.Add("FileSize", newFileSize);
obj.Add("FileExt", fileExt);
}
}
}catch(Exception ex)
{
msg=ex.Message;
obj= null;
}returnobj;
}///
///获取文件的大小///
///
///
public string GetFileSize(stringsFileFullName)
{
FileInfo fiInput= newFileInfo(sFileFullName);double len =fiInput.Length;string[] sizes = { "B", "KB", "MB", "GB"};int order = 0;while (len >= 1024 && order + 1
{
order++;
len= len / 1024;
}string filesize = String.Format("{0:0.##} {1}", len, sizes[order]);returnfilesize;
}#endregion}