一般情况.图片是不直接存到数据库的.而只是存了图片的名称.
存放图片的文件夹路径一般是固定的.
所 以 这 时 你 只 需 要 从 数 据 库 拿 出 图 片 名 称 . 直 接 在 页 面 中 这 么 写 就 OK 了 :<img
src="yourPath/<%=读出的图片名%>">
在上传的时候.首先当然是要判断上传文件是不是图片了.不建议去判断文件的后缀名.用
这个方法:
if(this.myfile.PostedFile.ContentType.ToString().ToLower().IndexOf("image")<0){
//
非图片文件
}
在对图片删除的时候.先去数据库里删除相关图片信息.然后再用:
System.IO.File.Delete(@"C:\ccc\ddd\eee.gif");
这里一定要用绝对路径.
上传图片到服务器上是网站开发中很常用的功能,它的实现也很简单,可以新建一个上
传
类
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");