上传图片

上传图片<script language="javascript" type="text/javascript"> document.title="上传图片 - "+document.title </script>

上传图片到服务器上是网站开发中很常用的功能,它的实现也很简单,可以新建一个上传类UpLoadAndSaveImage,这个类中包含三个函数UpLoadAndSaveCreateFilePathSaveToServer。使用时调用下面的UpLoadAndSave函数就可以了,该函数第一个参数为要上传的图片数据,第二个参数为上传的虚拟路径(相对路径),第三个参数为上传图片的格式,第四个参数为上传的物理路径。在这个函数中调用CreateFilePath函数产生随机的图片名称,最后再调用SaveToServer保存图片到服务器上。

public string UpLoadAndSave(byte[] data,ref string virPath,string fext,string physicPath)

         {

              //返回文件物理地址,修改虚拟地址

              if(data==null||virPath==null||fext==null||physicPath=="")

              {

                   throw new Exception("非法参数");

              }

              string rtnValue=SaveToServer(data,fext,physicPath,data.Length);

              virPath+=rtnValue;

              physicPath+=rtnValue;

              return physicPath;

         }

         private string CreateFilePath(string fext)

         {

              string filePath="";

              Random rd=new Random();

              filePath+=DateTime.Now.Year.ToString("0000");

              filePath+=DateTime.Now.Month.ToString("00");

              filePath+=DateTime.Now.Date.ToString("00");

              filePath+=DateTime.Now.Hour.ToString("00");

              filePath+=DateTime.Now.Minute.ToString("00");

              filePath+=DateTime.Now.Second.ToString("00");

              filePath+=DateTime.Now.Millisecond.ToString("00");

              filePath+=rd.Next(99).ToString("00");

              filePath+="."+fext;

              return filePath;

         }

         private string SaveToServer(byte[] data,string fext,string physicPath,int fileLen)

         {

              string filePath=CreateFilePath(fext);

              string rtnValue=filePath;

              filePath=filePath.Insert(0,@physicPath); 

              if(File.Exists(filePath))

              {

                   filePath=CreateFilePath(fext);

                   rtnValue=filePath;

              }

              FileStream fs=new FileStream(filePath,FileMode.CreateNew);

              fs.Write(data,0,fileLen);

              fs.Close();

              return rtnValue;

         }

在其他页面调用该上传类,见下面的实例:

         UpLoadAndSaveImage upload=new UpLoadAndSaveImage();

              try

              {

                   string virPath="UploadFiles/";

                   string physicPath=Server.MapPath(Request.ApplicationPath+"/"+"UploadFiles/");

                   string fext=this.File1.PostedFile.FileName;

                   if(fext.Length==0)

                   {

                       return;

                   }

                   fext=Path.GetExtension(fext).ToLower();

                   if(fext!=".jpg"&&fext!=".gif"&&fext!=".bmp"&&fext!=".doc"&&fext!=".rar"&&fext!=".zip"&&fext!=".jpeg")

                   {

                       Response.Write("<script>alert('Invalid file format,the file format must be jpg or jpeg or gif or bmp or doc or rar or zip')</script>");

                       return;

                   }

                   byte[] data=new byte[this.File1.PostedFile.ContentLength];

                   this.File1.PostedFile.InputStream.Read(data,0,this.File1.PostedFile.ContentLength);

                   physicPath=upload.UpLoadAndSave(data,ref virPath,fext,physicPath);

                   url=virPath;

                   if(Session["PhotoUrl"]==null)

                   {

                       ArrayList al=new ArrayList();

                       al.Add(physicPath);

                       Session["PhotoUrl"]=al;

                   }

                   else

                   {

                       ArrayList al2=(ArrayList)Session["PhotoUrl"];

                       al2.Add(physicPath);

                       Session["PhotoUrl"]=al2;

                   }

              }

              catch(Exception ex)

              {

                   Response.Write("<script>alert('"+ex.Message+"');</script>");

              }

  如果要指定上传图片的大小,可以在调用该上传类前生成,见下面的实例:

try

                   {

                       empPic = new Bitmap(File1.PostedFile.InputStream);

                   }

                   catch

                   {

                       Script.Alert("图片格式错误!");

                       return false;

                   }

                   Bitmap picSmall = new Bitmap(empPic,227,91); //生成图片大小

                   MemoryStream stream = new MemoryStream();

                   picSmall.Save(stream,ImageFormat.Jpeg);

                   byte[]  byteArray = stream.ToArray();

                   PathName1="Photo/";

                   PathName=Server.MapPath(Request.ApplicationPath+"/Photo/");

                   UpLoadAndSaveImage upimage=new UpLoadAndSaveImage();

                   PathName=upimage.UpLoadAndSave(byteArray,ref PathName1,".jpg",PathName

  • 0
    点赞
  • 0
    收藏
    觉得还不错? 一键收藏
  • 0
    评论

“相关推荐”对你有帮助么?

  • 非常没帮助
  • 没帮助
  • 一般
  • 有帮助
  • 非常有帮助
提交
评论
添加红包

请填写红包祝福语或标题

红包个数最小为10个

红包金额最低5元

当前余额3.43前往充值 >
需支付:10.00
成就一亿技术人!
领取后你会自动成为博主和红包主的粉丝 规则
hope_wisdom
发出的红包
实付
使用余额支付
点击重新获取
扫码支付
钱包余额 0

抵扣说明:

1.余额是钱包充值的虚拟货币,按照1:1的比例进行支付金额的抵扣。
2.余额无法直接购买下载,可以购买VIP、付费专栏及课程。

余额充值