上传大文件及图片公共类
大文件上传组件下载地址
#region UpLoad 上传大文件
//存放上传结果信息
public struct UpImgResult
{
public bool UpLoaded;
public string JScriptStr;
public string RetFileName;
public string FileFullPath;
}
//从本地上传图片到服务器
public static UpImgResult SaveImage(InputFile __UpImg, string __savePath)
{
UpImgResult Ret;
//验证文件类型
string FileType = System.IO.Path.GetExtension(__UpImg.FileName);
if (!FileType.ToUpper().Equals(".JPG") && !FileType.ToUpper().Equals(".GIF") && !FileType.ToUpper().Equals(".JPEG"))
{
Ret.UpLoaded = false;
Ret.JScriptStr = "<script language='javascript'>alert('文件类型不符,请选择 GIF,JPG,JPEG文件上传!');location.href='Join.aspx';</script>";
Ret.RetFileName = string.Empty;
Ret.FileFullPath = "";
return Ret;
}
//验证文件尺寸
if (__UpImg.FileContent.Length / 1024 > 150)
{
Ret.UpLoaded = false;
Ret.JScriptStr = "<script language='javascript'>alert('您上传的文件大小超出了150K限制!');location.href='Join.aspx';</script>";
Ret.RetFileName = string.Empty;
Ret.FileFullPath = "";
return Ret;
}
//验证文件比例
System.Drawing.Image upImage = System.Drawing.Image.FromStream(__UpImg.FileContent);
int width = upImage.Width;
int height = upImage.Height;
if (width / height > 1.6)
{
Ret.UpLoaded = false;
Ret.JScriptStr = "<script language='javascript'>alert('您上传的图片宽高比例大于8:5,不能上传!');location.href='Join.aspx';</script>";
Ret.RetFileName = string.Empty;
Ret.FileFullPath = "";
return Ret;
}
__UpImg.FileContent.Dispose();
System.Random rd = new Random();
string FileName = DateTime.Today.ToShortDateString();
FileName += "-" + DateTime.Now.Hour.ToString() + "-" + DateTime.Now.Minute.ToString();
FileName += "-" + rd.Next(1000, 9999).ToString() + FileType;
string fullPath = HttpContext.Current.Request.PhysicalApplicationPath + __savePath.Replace('/', '\\');
try
{
__UpImg.MoveTo(Path.Combine(fullPath, FileName), MoveToOptions.Overwrite);
UpImgResult Ret1 = CropImage(Path.Combine(HttpContext.Current.Request.PhysicalApplicationPath + __savePath.Replace('/','\\'), FileName));
if (Ret1.UpLoaded)
{
Ret.UpLoaded = true;
Ret.JScriptStr = "<script language='javascript'>alert('上传成功!');</script>";
Ret.RetFileName = FileName;
Ret.FileFullPath = fullPath + FileName;
return Ret;
}
return Ret1;
}
catch (System.Exception ex)
{
Ret.UpLoaded = false;
Ret.JScriptStr = ex.Message;
Ret.RetFileName = string.Empty;
Ret.FileFullPath = "";
return Ret;
}
}
//上传网络图片到服务器
public static UpImgResult SaveHttpImage(string __FileName, string __savePath)
{
UpImgResult Ret;
WebClient client = new WebClient();
string FileType = System.IO.Path.GetExtension(__FileName);
if (!FileType.ToUpper().Equals(".JPG") && !FileType.ToUpper().Equals(".GIF") && !FileType.ToUpper().Equals(".JPEG"))
{
Ret.UpLoaded = false;
Ret.JScriptStr = "<script language='javascript'>alert('文件类型不符,请选择 GIF,JPG,JPEG文件上传!');history.back();</script>";
Ret.RetFileName = string.Empty;
Ret.FileFullPath = "";
return Ret;
}
System.Random rd = new Random();
string FileName = DateTime.Today.ToShortDateString();
FileName += "-" + DateTime.Now.Hour.ToString() + "-" + DateTime.Now.Minute.ToString();
FileName += "-" + rd.Next(1000, 9999).ToString() + FileType;
string fullPath = HttpContext.Current.Request.PhysicalApplicationPath + __savePath + "\\";
try
{
client.DownloadFile(__FileName, Path.Combine(fullPath, FileName));
UpImgResult Ret1 = CropImage(Path.Combine(fullPath + "\\", FileName));
if (Ret1.UpLoaded)
{
Ret.UpLoaded = true;
Ret.JScriptStr = "<script language='javascript'>alert('上传成功!');</script>";
Ret.RetFileName = FileName;
Ret.FileFullPath = fullPath + FileName;
return Ret;
}
return Ret1;
}
catch (System.Exception ex)
{
Ret.UpLoaded = false;
Ret.JScriptStr = ex.Message;
Ret.RetFileName = FileName;// Path.Combine(HttpContext.Current.Request.PhysicalApplicationPath + __savePath + "\\", FileName);
Ret.FileFullPath = "";
return Ret;
}
Ret.UpLoaded = true;
Ret.JScriptStr = "<script language='javascript'>alert('上传成功!');</script>";
Ret.RetFileName = FileName;
Ret.FileFullPath = fullPath + FileName;
return Ret;
}
//返回结果初始化函数
public static UpImgResult CropImage(string __FileName)
{
UpImgResult Ret;
Ret.UpLoaded = true;
Ret.JScriptStr = "";
Ret.RetFileName = "";
Ret.FileFullPath = "";
return Ret;
}
//大文件上传
public static UpImgResult SaveFile(InputFile __UpFile, string __savePath)
{
UpImgResult Ret;
if (__UpFile.ContentLength == 0)
{
Ret.UpLoaded = false;
Ret.JScriptStr = "<script language=javascript>alert('请检查文件地址或文件大小是否为0');history.back();</script>";
Ret.RetFileName = string.Empty;
Ret.FileFullPath = "";
return Ret;
}
string FileType = System.IO.Path.GetExtension(__UpFile.FileName);
System.Random rd = new Random();
string FileName = DateTime.Today.ToShortDateString();
FileName += "-" + DateTime.Now.Hour.ToString() + "-" + DateTime.Now.Minute.ToString();
FileName += "-" + rd.Next(1000, 9999).ToString() + FileType;
string fullPath = HttpContext.Current.Request.PhysicalApplicationPath + __savePath + "\\";
try
{
__UpFile.MoveTo(Path.Combine(fullPath, FileName), MoveToOptions.Overwrite);
}
catch (System.Exception ex)
{
Ret.UpLoaded = false;
Ret.JScriptStr = ex.Message;
Ret.RetFileName = string.Empty;
Ret.FileFullPath = "";
return Ret;
}
Ret.UpLoaded = true;
Ret.JScriptStr = "<script language='javascript'>alert('上传成功!');</script>";
Ret.RetFileName = FileName;
Ret.FileFullPath = fullPath + FileName;
return Ret;
}
/// <summary>
/// 上传普通文件,不生成缩略图, 使用.net自己的上传
/// </summary>
/// <returns></returns>
public static int AddGeneralFile(HttpFileCollection files, out string FileUrl, out string ExtName, out string FileName, out bool bIsPic)
{
FileUrl = "";
ExtName = "";
FileName = "";
bIsPic = false;
if (files != null)
{
for (int i = 0; i < files.Count; i++)
{
HttpPostedFile part = files[i];
string strContentType = part.ContentType;
int iFileSize = (int)part.ContentLength;//文件大小
string strFullFileName = part.FileName;//文件路径加名称
ExtName = System.IO.Path.GetExtension(strFullFileName).ToLower();
if (ExtName == ".jpg" || ExtName == ".jpeg" || ExtName == ".bmp" || ExtName == ".gif")
bIsPic = true;
int iPosition = strFullFileName.LastIndexOf("\\");
if (iPosition > 0)
{
strFullFileName = strFullFileName.Remove(0, iPosition + 1);
FileName = strFullFileName;
if (iFileSize <= 0)
{
return -1;//无文件上传,或文件太小。
}
string strPictureURL = "";
string strImageSaveURL = "";// HttpContext.Current.Server.MapPath(GW.BLL.Components.GetConfigSettings("UploadPath"));
string strFileName = DateTime.Now.Ticks.ToString();//新的文件名
strPictureURL = strImageSaveURL + "\\" + DateTime.Now.ToString("yyyyMMdd") + "\\"; //普通文件保存路径
FileUrl = "";// GW.BLL.Components.GetConfigSettings("UploadPath") + "/" + DateTime.Now.ToString("yyyyMMdd") + "/" + strFileName + ExtName;
if (!Directory.Exists(strPictureURL)) Directory.CreateDirectory(strPictureURL);
strPictureURL = strPictureURL + strFileName + ExtName;
try
{
part.SaveAs(strPictureURL); //保存
}
catch
{
return -2;//保存文件失败,请检查路径
}
}
}
}
return 0;
}
#endregion
//调用实例
/// <summary>
/// 上传图片单击事件
/// </summary>
/// <param name="sender"></param>
/// <param name="e"></param>
protected void btnUpload_Click(object sender, EventArgs e)
{
if (uploadFile.FileName != "")
{
PageHelper.UpImgResult upResult = new PageHelper.UpImgResult();
upResult.JScriptStr = "<script language='javascript'>alert('上传失败!');</script>";
upResult = PageHelper.SaveImage(uploadFile, PageHelper.UploadPath);
ClientScript.RegisterStartupScript(GetType(), "", upResult.JScriptStr);
if (upResult.UpLoaded)
{
m_UpFileName = upResult.RetFileName;
}
imgPreview.Src = PageHelper.UploadPath + upResult.RetFileName;
}
}

浙公网安备 33010602011771号