首页  ·  知识 ·  云计算
.Net图片上传的一个类库的源码
佚名  http://www.bjcan.com/hengxing/  综合  编辑:dezai  图片来源:网络
上传图片到服务器上是网站开发中很常用的功能,它的实现也很简单,可以新建一个上传类UpLoadAndSaveImage,这个类中包含三个函数UpLoadAndSave,CreateFileP

上传图片到服务器上是网站开发中很常用的功能,它的实现也很简单,可以新建一个上传类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("");
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("");
}

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

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);

本文作者:佚名 来源:http://www.bjcan.com/hengxing/
CIO之家 www.ciozj.com 微信公众号:imciow
   
免责声明:本站转载此文章旨在分享信息,不代表对其内容的完全认同。文章来源已尽可能注明,若涉及版权问题,请及时与我们联系,我们将积极配合处理。同时,我们无法对文章内容的真实性、准确性及完整性进行完全保证,对于因文章内容而产生的任何后果,本账号不承担法律责任。转载仅出于传播目的,读者应自行对内容进行核实与判断。请谨慎参考文章信息,一切责任由读者自行承担。
延伸阅读