谁有图片上传代码/加注释给份 用来研究 源码谢谢先 解决方案 » 免费领取超大流量手机卡,每月29元包185G流量+100分钟通话, 中国电信官方发货 SaveAs 就可以了?还想要什么? /// <summary> /// 上传多个图片 /// by minjiang 07-08-01 /// </summary> /// <param name="sSavePath">原图路径</param> /// <param name="sSaveSmallPath">小图路径</param> /// <param name="preFileName"></param> /// <param name="iMaxSize">大小</param> /// <param name="iWidth">宽</param> /// <param name="iHeight">高</param> /// <param name="intSucess">统计有多少个图片已经成功上传</param> /// <param name="arrFileNames">保存原图名称数组</param> /// <param name="arrSmallFileNames">保存小图名称数组</param> /// <returns></returns> public string[] upPicture(string sSavePath, string sSaveSmallPath, string preFileName, int iMaxSize, int iWidth, int iHeight,int intSucess, out ArrayList arrFileNames, out ArrayList arrSmallFileNames) { arrFileNames = new ArrayList(); arrSmallFileNames = new ArrayList(); HttpFileCollection files = HttpContext.Current.Request.Files; string strfileoldpath; //上传原图片的保存路径 string strfileoldname;//图片保存后名称 string strfilesmallpath; //小图片的保存路径 string strfilesmallname;//小图片保存后名称 string fileName, fileExtension; string[] arrayImageUrl = new string[4] { "", "", "", "" }; int iSucess = 0;//统计有多少个图片已经成功上传 try { for (int iFile = 0; iFile < files.Count; iFile++) { ///'检查文件扩展名字 HttpPostedFile postedFile = files[iFile]; //没有文件上传 if (postedFile.FileName == "") { arrayImageUrl[2] = "errorNoFile"; continue; } fileName = System.IO.Path.GetFileName(postedFile.FileName); if (fileName == "") { arrayImageUrl[2] = "errorNoFile"; continue; } fileExtension = System.IO.Path.GetExtension(fileName); //判断上传文件类型 string filetype = postedFile.ContentType; //判断是否图片 if ((filetype.IndexOf("image") == -1) || (fileExtension.Trim().ToLower() != ".jpeg") && (fileExtension.Trim().ToLower() != ".jpg") && (fileExtension.Trim().ToLower() != ".gif")) { arrayImageUrl[2] = "errorT"; continue; } //判断是否图片大小超过上限 int MaxFile = 0; MaxFile = postedFile.ContentLength; if (MaxFile > (iMaxSize * 1024)) { arrayImageUrl[2] = "errorb"; continue; } //检查保存文件的目录是否存在,否则创建 strfileoldpath = CheckProductPictureML(sSavePath); strfilesmallpath = CheckProductPictureML(sSaveSmallPath); //为文件命名,然后保存 Random ra = new Random(); strfileoldname = preFileName + "_" + ra.Next(100000, 999999).ToString() + fileExtension; strfilesmallname = "s_" + strfileoldname; strfileoldpath += "\\" + strfileoldname; strfilesmallpath += "\\" + strfilesmallname; //保存原始图像 postedFile.SaveAs(strfileoldpath); //把上传的图片另存一个小图 if (this.DealImage(strfileoldpath, strfilesmallpath, fileExtension, iWidth, iHeight)) { arrayImageUrl[0] = sSavePath + "/" + strfileoldname; arrayImageUrl[1] = sSaveSmallPath + "/" + strfilesmallname; arrFileNames.Add(arrayImageUrl[0].ToString()); arrSmallFileNames.Add(arrayImageUrl[1].ToString()); iSucess += 1; } else { arrayImageUrl[2] = "error"; continue; } } //统计已经成功上传的图片数,如果大于等于1则说明上传成功 if (iSucess >= intSucess) { arrayImageUrl[2] = ""; } else { arrayImageUrl[2] = "error"; } } catch { arrayImageUrl[2] = "error"; return arrayImageUrl; } return arrayImageUrl; } /// <summary> /// 检查图片的保存路径是否存在 /// </summary> /// <returns></returns> public string CheckProductPictureML(string sSavePath) { string strml=this.Server.MapPath(sSavePath); if(Directory.Exists(strml)==false) { Directory.CreateDirectory(strml); } #region maybe Use /***************************************************************** //年 strml+="\\"+DateTime.Now.Year.ToString(); if(Directory.Exists(strml)==false) { Directory.CreateDirectory(strml); } //月 strml+="\\"+DateTime.Now.Month.ToString(); if(Directory.Exists(strml)==false) { Directory.CreateDirectory(strml); } //日 strml+="\\"+DateTime.Now.Day.ToString(); if(Directory.Exists(strml)==false) { Directory.CreateDirectory(strml); } *****************************************************************/ #endregion return strml; } ///<summary> ///图片处理(另存一个小图片,并设定图片的最大宽度,最大高度,进行等比缩放) /// by fycooer /// 2007-01-20 ///</summary> ///<return></return> private bool DealImage(string Imagepath, string Imagesmallpath, string Extension,int iWidth,int iHeight) { int iWidthTemp=iWidth; int iHeightTemp=iHeight; //by minjiang 07-08-24 double dHeight=(double )iHeight ; double dWidth=(double )iWidth ; try { System.Drawing.Image myimage = System.Drawing.Image.FromFile(Imagepath); //int iHeight; //int iWidth = 180; //如果上传的图片的宽大于设置的宽或者图片的高大于设置的高 if (myimage.Width > iWidth || myimage.Height > iHeight) { if (((double)myimage.Width) / (double)iWidth > ((double)myimage.Height) / (double)iHeight) { // iHeight = Convert.ToInt16(((double)(iWidth / myimage.Width) * myimage.Height)); //by minjiang 07-08-08 修改 dHeight = (double)iWidth / (double)myimage.Width; dHeight = dHeight * myimage.Height; iHeight = Convert.ToInt32(dHeight); } else { //iWidth = Convert.ToInt16(((double)iHeight / myimage.Height * myimage.Width)); //iWidth = myimage.Width; //iHeight = myimage.Height; //by minjiang 07-08-08 修改 dWidth = (double)iHeight / (double)myimage.Height; dWidth = dWidth * myimage.Width; iWidth = Convert.ToInt32(dWidth); } if (iWidth > iWidthTemp) { iWidth = iWidthTemp; } if (iHeight > iHeightTemp) { iHeight = iHeightTemp; } } else { //如果图片的宽或者是高都没有大于设定的值,则不进行任何缩放 //by minjiang 07-08-24 解决图片的缩略图片大小问题 iWidth = myimage .Width ; iHeight = myimage .Height ; } System.Drawing.Bitmap smallimage = new Bitmap(myimage, iWidth, iHeight); switch (Extension) { case ".gif": smallimage.Save(Imagesmallpath, System.Drawing.Imaging.ImageFormat.Gif); break; default: smallimage.Save(Imagesmallpath, System.Drawing.Imaging.ImageFormat.Jpeg); break; } smallimage.Dispose(); myimage.Dispose(); return true; } catch { return false; } } //定义一个变量,取全部文件名 string fileFullname=this.File1.PostedFile.FileName; //取出当前日期 string dataName= DateTime.Now.ToString("yyyyMMddhhmmss"); //取文件扩展名 string fileName=fileFullname.Substring(fileFullname.LastIndexOf("\\")+1); //截取上传文件的扩展名既"."后面的字母所以+1 string type=fileFullname.Substring(fileFullname.LastIndexOf(".")+1); //类型控制,想上传什么类型,可以在这里类似添加 if(type=="jpg"||type=="gif"||type=="bmp") { this.File1.PostedFile.SaveAs(Server.MapPath("pic")+"\\"+dataName+fileName); Response.Write("<script language='javascript'>alert('上传成功!');</script>"); //image1输出上传图片,可以在这里控制图片大小 this.Image1.ImageUrl="pic"+"\\"+dataName+fileName; } else { Response.Write("<script language='javascript'>alert('你选的文件类型不符!');</script>"); } see:http://www.aspxboy.com/private/218/default.aspx 如何把js用正则转成HTML代码 关于设置权限问题 关于子线程的异常处理 我在项目中用了performanceCounter1 组件,可是不能调试,生成也没问题。。 怎么停止.net的服务? 各位帮忙,着急!搞不定就会不了家了 C#输出文件的问题 怎样对整个解决方案 的XX.cs文件进行组织uing 移除和排序? 这两个函数是什么意思?(c#) C#在拖放过程中如何处理鼠标悬停? 停止线程的问题 怎样通过线程名字 来结束线程
/// 上传多个图片
/// by minjiang 07-08-01
/// </summary>
/// <param name="sSavePath">原图路径</param>
/// <param name="sSaveSmallPath">小图路径</param>
/// <param name="preFileName"></param>
/// <param name="iMaxSize">大小</param>
/// <param name="iWidth">宽</param>
/// <param name="iHeight">高</param>
/// <param name="intSucess">统计有多少个图片已经成功上传</param>
/// <param name="arrFileNames">保存原图名称数组</param>
/// <param name="arrSmallFileNames">保存小图名称数组</param>
/// <returns></returns> public string[] upPicture(string sSavePath, string sSaveSmallPath, string preFileName, int iMaxSize, int iWidth, int iHeight,int intSucess, out ArrayList arrFileNames, out ArrayList arrSmallFileNames)
{
arrFileNames = new ArrayList();
arrSmallFileNames = new ArrayList();
HttpFileCollection files = HttpContext.Current.Request.Files;
string strfileoldpath; //上传原图片的保存路径
string strfileoldname;//图片保存后名称
string strfilesmallpath; //小图片的保存路径
string strfilesmallname;//小图片保存后名称 string fileName, fileExtension;
string[] arrayImageUrl = new string[4] { "", "", "", "" };
int iSucess = 0;//统计有多少个图片已经成功上传
try
{
for (int iFile = 0; iFile < files.Count; iFile++)
{
///'检查文件扩展名字
HttpPostedFile postedFile = files[iFile];
//没有文件上传
if (postedFile.FileName == "")
{
arrayImageUrl[2] = "errorNoFile";
continue; }
fileName = System.IO.Path.GetFileName(postedFile.FileName);
if (fileName == "")
{
arrayImageUrl[2] = "errorNoFile";
continue; } fileExtension = System.IO.Path.GetExtension(fileName);
//判断上传文件类型
string filetype = postedFile.ContentType;
//判断是否图片
if ((filetype.IndexOf("image") == -1) || (fileExtension.Trim().ToLower() != ".jpeg") && (fileExtension.Trim().ToLower() != ".jpg") && (fileExtension.Trim().ToLower() != ".gif"))
{
arrayImageUrl[2] = "errorT";
continue;
} //判断是否图片大小超过上限
int MaxFile = 0;
MaxFile = postedFile.ContentLength;
if (MaxFile > (iMaxSize * 1024))
{
arrayImageUrl[2] = "errorb";
continue;
} //检查保存文件的目录是否存在,否则创建
strfileoldpath = CheckProductPictureML(sSavePath);
strfilesmallpath = CheckProductPictureML(sSaveSmallPath); //为文件命名,然后保存
Random ra = new Random(); strfileoldname = preFileName + "_" + ra.Next(100000, 999999).ToString() + fileExtension;
strfilesmallname = "s_" + strfileoldname; strfileoldpath += "\\" + strfileoldname;
strfilesmallpath += "\\" + strfilesmallname;
//保存原始图像
postedFile.SaveAs(strfileoldpath); //把上传的图片另存一个小图
if (this.DealImage(strfileoldpath, strfilesmallpath, fileExtension, iWidth, iHeight))
{
arrayImageUrl[0] = sSavePath + "/" + strfileoldname;
arrayImageUrl[1] = sSaveSmallPath + "/" + strfilesmallname;
arrFileNames.Add(arrayImageUrl[0].ToString());
arrSmallFileNames.Add(arrayImageUrl[1].ToString());
iSucess += 1; }
else
{
arrayImageUrl[2] = "error";
continue;
}
}
//统计已经成功上传的图片数,如果大于等于1则说明上传成功
if (iSucess >= intSucess)
{ arrayImageUrl[2] = ""; }
else
{ arrayImageUrl[2] = "error"; }
}
catch
{
arrayImageUrl[2] = "error";
return arrayImageUrl;
}
return arrayImageUrl;
}
/// 检查图片的保存路径是否存在
/// </summary>
/// <returns></returns>
public string CheckProductPictureML(string sSavePath)
{
string strml=this.Server.MapPath(sSavePath);
if(Directory.Exists(strml)==false)
{
Directory.CreateDirectory(strml);
} #region maybe Use /***************************************************************** //年
strml+="\\"+DateTime.Now.Year.ToString();
if(Directory.Exists(strml)==false)
{
Directory.CreateDirectory(strml);
}
//月
strml+="\\"+DateTime.Now.Month.ToString();
if(Directory.Exists(strml)==false)
{
Directory.CreateDirectory(strml);
}
//日
strml+="\\"+DateTime.Now.Day.ToString();
if(Directory.Exists(strml)==false)
{
Directory.CreateDirectory(strml);
}
*****************************************************************/
#endregion return strml;
}
///图片处理(另存一个小图片,并设定图片的最大宽度,最大高度,进行等比缩放)
/// by fycooer
/// 2007-01-20
///</summary>
///<return></return>
private bool DealImage(string Imagepath, string Imagesmallpath, string Extension,int iWidth,int iHeight)
{
int iWidthTemp=iWidth;
int iHeightTemp=iHeight;
//by minjiang 07-08-24
double dHeight=(double )iHeight ;
double dWidth=(double )iWidth ;
try
{
System.Drawing.Image myimage = System.Drawing.Image.FromFile(Imagepath); //int iHeight;
//int iWidth = 180;
//如果上传的图片的宽大于设置的宽或者图片的高大于设置的高
if (myimage.Width > iWidth || myimage.Height > iHeight)
{
if (((double)myimage.Width) / (double)iWidth > ((double)myimage.Height) / (double)iHeight)
{
// iHeight = Convert.ToInt16(((double)(iWidth / myimage.Width) * myimage.Height));
//by minjiang 07-08-08 修改 dHeight = (double)iWidth / (double)myimage.Width;
dHeight = dHeight * myimage.Height;
iHeight = Convert.ToInt32(dHeight);
}
else
{
//iWidth = Convert.ToInt16(((double)iHeight / myimage.Height * myimage.Width));
//iWidth = myimage.Width;
//iHeight = myimage.Height;
//by minjiang 07-08-08 修改 dWidth = (double)iHeight / (double)myimage.Height;
dWidth = dWidth * myimage.Width;
iWidth = Convert.ToInt32(dWidth);
}
if (iWidth > iWidthTemp)
{
iWidth = iWidthTemp;
}
if (iHeight > iHeightTemp)
{
iHeight = iHeightTemp; }
}
else
{
//如果图片的宽或者是高都没有大于设定的值,则不进行任何缩放
//by minjiang 07-08-24 解决图片的缩略图片大小问题
iWidth = myimage .Width ;
iHeight = myimage .Height ;
}
System.Drawing.Bitmap smallimage = new Bitmap(myimage, iWidth, iHeight);
switch (Extension)
{
case ".gif":
smallimage.Save(Imagesmallpath, System.Drawing.Imaging.ImageFormat.Gif);
break;
default:
smallimage.Save(Imagesmallpath, System.Drawing.Imaging.ImageFormat.Jpeg);
break;
}
smallimage.Dispose();
myimage.Dispose();
return true;
}
catch
{
return false;
}
}
string fileFullname=this.File1.PostedFile.FileName;
//取出当前日期
string dataName= DateTime.Now.ToString("yyyyMMddhhmmss");
//取文件扩展名
string fileName=fileFullname.Substring(fileFullname.LastIndexOf("\\")+1);
//截取上传文件的扩展名既"."后面的字母所以+1
string type=fileFullname.Substring(fileFullname.LastIndexOf(".")+1);
//类型控制,想上传什么类型,可以在这里类似添加
if(type=="jpg"||type=="gif"||type=="bmp")
{
this.File1.PostedFile.SaveAs(Server.MapPath("pic")+"\\"+dataName+fileName);
Response.Write("<script language='javascript'>alert('上传成功!');</script>");
//image1输出上传图片,可以在这里控制图片大小
this.Image1.ImageUrl="pic"+"\\"+dataName+fileName;
}
else
{
Response.Write("<script language='javascript'>alert('你选的文件类型不符!');</script>");
}