private string UpImages(out int flag)
{
flag = 0;
string subPath = String.Empty;
if (this.upimage.Value == "")
{
model.NwCs_PicUrl = "blank";
}
else
{
string updatefilename = this.upimage.Value.ToString();
string tempstring = null;
if (updatefilename.IndexOf('.') > 0)
{
string[] name = updatefilename.Split('.');
tempstring = name[name.Length - 1];
}
if (tempstring == "jpg" || tempstring == "JPG" || tempstring == "gif" || tempstring == "GIF")
{
string filePath = "/UpSPContract/"; //+System.DateTime.Now.ToString("yyyyMMdd")+"/";
if (!Directory.Exists(filePath))
{
Directory.CreateDirectory(filePath); // 如无该文件夹,则创建
}
string fileName = "VS" + System.DateTime.Now.ToString("yyyyMMddhhmmss");
string fileType = String.Empty; string netPath = HttpContext.Current.Request.MapPath(HttpContext.Current.Request.ApplicationPath);
CommonFile.FileHelper.SaveFiles(upimage, netPath + filePath, fileName, ref fileType);
subPath = subPath + fileName + fileType;
//ViewState["ConPath"] = netPath + filePath + subPath;
//Response.Write("<script>alert('上传成功!')</script>");
}
else
{
flag = 1;
Response.Write("<script>alert('图片格式不符合!')</script>");
}
} return subPath;这里面的"/UpSPContract/"; 是什么意思,是不是目录文件夹。然后数据是哪里读的。前台要去哪里读取。是不是应该去读数据库里面的。不过。我看了一个项目。好像都没有什么数据库操作。是不是可以直接读文件名。。我很菜啊。。
{
flag = 0;
string subPath = String.Empty;
if (this.upimage.Value == "")
{
model.NwCs_PicUrl = "blank";
}
else
{
string updatefilename = this.upimage.Value.ToString();
string tempstring = null;
if (updatefilename.IndexOf('.') > 0)
{
string[] name = updatefilename.Split('.');
tempstring = name[name.Length - 1];
}
if (tempstring == "jpg" || tempstring == "JPG" || tempstring == "gif" || tempstring == "GIF")
{
string filePath = "/UpSPContract/"; //+System.DateTime.Now.ToString("yyyyMMdd")+"/";
if (!Directory.Exists(filePath))
{
Directory.CreateDirectory(filePath); // 如无该文件夹,则创建
}
string fileName = "VS" + System.DateTime.Now.ToString("yyyyMMddhhmmss");
string fileType = String.Empty; string netPath = HttpContext.Current.Request.MapPath(HttpContext.Current.Request.ApplicationPath);
CommonFile.FileHelper.SaveFiles(upimage, netPath + filePath, fileName, ref fileType);
subPath = subPath + fileName + fileType;
//ViewState["ConPath"] = netPath + filePath + subPath;
//Response.Write("<script>alert('上传成功!')</script>");
}
else
{
flag = 1;
Response.Write("<script>alert('图片格式不符合!')</script>");
}
} return subPath;这里面的"/UpSPContract/"; 是什么意思,是不是目录文件夹。然后数据是哪里读的。前台要去哪里读取。是不是应该去读数据库里面的。不过。我看了一个项目。好像都没有什么数据库操作。是不是可以直接读文件名。。我很菜啊。。
解决方案 »
- vs2008 智能感知问题
- 未能找到路径“D:\website\WebSite1\”的一部分。
- 求助各路英雄
- 关于对csdn网站的疑惑,请指点!
- ListView in VS2008 for C# 3.5 增加,修改,删除问题
- 帮我看看这个SQL语句,看一下哪里错了。
- 一个ASPX页面要用到多个.cs后台文件,怎么办?
- 强烈求教:asp.net的运行机制与原理?(iis,global,webconfig,httphandler,httpmoduler……)
- 发帖攒分~
- 自定义用户控件如何传值
- [求助] VS2008发布网站后,如何设置网站根目录的权限?XP系统。
- 有个问题始终不明白,开发门户网站,是用JSP好,还是ASP.NET好??请大侠指导··
数据图片也是上传到服务器的文件夹下的,和数据库没关系
告诉你,详细运作过程,
1,点击浏览按钮,选定好上传图片,不管图片是存放在什么位置的(存放在什么地方一点关系都没有)
2,点击上传按钮,图片就会上传到/UpSPContract下,然后自动生成一个文件名
(string filePath = "/UpSPContract/"; //+System.DateTime.Now.ToString("yyyyMMdd")+"/"; 就是这句起的作用)
3,将取得图片地址,导入数据库
4,附上代码碎片(如下)
try
{
string test = Server.MapPath("ImageFiles/" + DateTime.Now.ToString("yyyy-MM-dd")); //用来生成文件夹
if (!Directory.Exists(test))
{
Directory.CreateDirectory(test);
}
if (FileUpload1.PostedFile.FileName != "")
{
string imgname = FileUpload1.PostedFile.FileName;
string imgType = imgname.Substring(imgname.LastIndexOf(".") + 1);
string quanname = DateTime.Now.ToString("yyyyMMddHHmmss") + imgname.LastIndexOf("\\") + "." + imgType;
if ("gif" != imgType && "jpg" != imgType && "GIF" != imgType && "JPG" != imgType)
{
Response.Write("<script>alert('请选择gif,jpg格式的文件!');</script>");
return;
} string imgurl = "ImageFiles/" + DateTime.Now.ToString("yyyy-MM-dd") + "/" + quanname;
FileUpload1.PostedFile.SaveAs(Server.MapPath(imgurl));
SqlConnection con = new SqlConnection(System.Configuration.ConfigurationSettings.AppSettings["con"]);
SqlCommand cmd = new SqlCommand("update students set image=@image where xuehao='002'", con);
cmd.Parameters.Add("@image", imgurl);
cmd.Connection.Open();
cmd.ExecuteNonQuery();
cmd.Connection.Close();
SqlCommand cmd1 = new SqlCommand("select * from students where xuehao='002'", con);
cmd1.Connection.Open();
SqlDataReader dr = cmd1.ExecuteReader(); if (dr.Read())
{
Response.Write("<script>alert('头像上传成功!');</script>");
string hui = dr["image"].ToString();
this.Image1.ImageUrl = hui;
}
cmd1.Connection.Close(); }
else
{
Response.Write("<script>alert('请选择文件上传!');</script>");
}
} catch (Exception ex)
{
throw ex;
}
string image=dr["image"].ToString();
if(!(image==""))
{
this.Image1.ImageUrl = image;
}