Net 图片上传的一个类库的源码
来源:互联网 发布:手机网站制作软件 编辑:程序博客网 时间:2024/05/06 22:28
上传图片到服务器上是网站开发中很常用的功能,它的实现也很简单,可以新建一个上传类UpLoadAndSaveImage,这个类中包含三个函数UpLoadAndSave,CreateFilePath,SaveToServer。使用时调用下面的UpLoadAndSave函数就可以了,该函数第一个参数为要上传的图片数据,第二个参数为上传的虚拟路径(相对路径),第三个参数为上传图片的格式,第四个参数为上传的物理路径。在这个函数中调用CreateFilePath函数产生随机的图片名称,最后再调用SaveToServer保存图片到服务器上。
public string UpLoadAndSave(byte[] data,refstring 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=newbyte[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);
- Net 图片上传的一个类库的源码
- .Net 图片上传的一个类库的源码
- asp.Net_图片上传的一个类库的源码
- .net上传图片的处理类库(缩略图、水印)
- .net 删除上传的图片
- .net中的上传文件的一个类
- 在ASP.NET中上传图片并生成缩略图的C#源码
- 在ASP.NET中上传图片并生成缩略图的C#源码 (转载)
- 在ASP.NET中上传图片并生成缩略图的C#源码
- 在ASP.NET中上传图片并生成缩略图的C#源码
- 在ASP.NET中上传图片并生成缩略图的C#源码
- 在ASP.NET中上传图片并生成缩略图的C#源码
- 在ASP.NET中上传图片并生成缩略图的C#源码(一)
- 在ASP.NET中上传图片并生成缩略图的C#源码
- 在ASP.NET中上传图片并生成缩略图的C#源码
- 在ASP.NET中上传图片并生成缩略图的C#源码
- 自己写的一个图片上传的类
- VS2005图片上传源码 ASP.NET 图片上传源码 asp:FileUpload 图片上传源码
- MFC中点线面的绘制
- MFC窗口的颜色和字体的设置
- MFC中对窗口贴图
- 疯狂的弹簧
- 图形的保存和重绘
- Net 图片上传的一个类库的源码
- 软件在于应用
- 一个小小的杀毒程序
- 今天David Solomon的为期三天的Windows Internal培训刚结束
- 链路层
- 三分屏课件打包器远程教育版(V1.0)
- windows正版验证问题。。。
- Word文档加密器(V4.0)
- 数据结构-二叉树算法