FileUpload上传图片的问题、、、、、 VS2005中FileUpload怎样选中图片时,实现预览效果。FileUpload是服务器控件。高手,求救啊。。 解决方案 » 免费领取超大流量手机卡,每月29元包185G流量+100分钟通话, 中国电信官方发货 不行,能否贴出代码,label显示的还是选中的绝对路径 string type; if (ddlist.SelectedItem.Value == "") { ScriptManager.RegisterStartupScript(this, this.GetType(), "", "alert('你还没有选择专栏');window.location.href='User.aspx';", true); return; } else { type = ddlist.SelectedItem.Value; ViewState["type"] = type; ddlist.Enabled = false; } Boolean fileOK = true; if (fUfile.HasFile) { if (fileOK) { try { string Date = System.DateTime.Now.ToString("yyMMddhhmmss"); string subFie = fUfile.FileName.Substring(fUfile.FileName.LastIndexOf("."), 4); string sLocalPath = "../common/wwjeditor/UploadFiles/"; string folder = Server.MapPath(sLocalPath) + type; if (!Directory.Exists(folder)) Directory.CreateDirectory(folder); string path = folder + "\\" + Date + subFie; string fileName = fUfile.FileName; int size = fUfile.PostedFile.ContentLength; // string userName = type; Multimedia mul = new Multimedia(); mul.Name = fileName; mul.Size = size; mul.Path = "common/wwjeditor/UploadFiles/" + type + "/" + Date + subFie; mul.Series = 1; //上传到文件夹 sLocalPath = "../common/wwjeditor/UploadFiles/" + type; path = Server.MapPath(sLocalPath) + "\\" + mul.Path.Substring(mul.Path.LastIndexOf("/") + 1); if (!File.Exists(path)) { fUfile.SaveAs(path); } if (ViewState["list"] != null) { List<Multimedia> list =(List<Multimedia>) ViewState["list"]; mul.Series = list.Count + 1; list.Add(mul); gvAccessory.DataSource = list; gvAccessory.DataBind(); } else { List<Multimedia> list = new List<Multimedia>(); list.Add(mul); ViewState["list"] = list; gvAccessory.DataSource = list; gvAccessory.DataBind(); } } catch (System.Exception ex) { Response.Write("<script>alert('" + ex.Message + "');</script>"); return; } } } else { Response.Write("<script>alert('上传的文件不符合要求!');</script>"); return; } 二进制图片显示上传二进制if (FileUpLogo.HasFile){ //取得上传文件的大小 int FileLen = FileUpLogo.PostedFile.ContentLength; Byte[] FileData = new Byte[FileLen]; //创建访问客户端上传文件的对象 HttpPostedFile hp = FileUpLogo.PostedFile; //创建数据流对象 System.IO.Stream sr = hp.InputStream; //将图片数据放到FileData数组对象实例中,0代表数组指针的起始位置,FileLen代表指针的结束位置 sr.Read(FileData, 0, FileLen); //将FileData 赋值给实体 brandModel.fld_logo = FileData;}或者HttpPostedFile upFile = up_file.PostedFile;//HttpPostedFile对象,用来读取上传图片的属性 fileLength = upFile.ContentLength;//记录文件的长度 try { if(fileLength==0)//当文件长度为0的时候 { txtMessage.Text = "请选择要上传的文件!"; } else { byte[] fileByte = new byte[fileLength];//用图片的长度来初始化一个字节数组存储临时的图片文件 Stream fileStream = upFile.InputStream;//建立文件流对象 fileStream.Read(fileByte,0,fileLength);//读取图片数据到临时存储体fileByte,0为数据指针位置,fileLength为数据长度 string connString = "Data Source=192.168.1.250;database=image;uid=pwqzc;pwd=cn0088"; SqlConnection conn = new SqlConnection(connString);//初始化数据库连接 string insertStr = "insert into image (image_data,image_content_type,image_description,image_size) values (@image_data,@image_content_type,@image_description,@image_size)"; //插入数据库语句 SqlCommand comm = new SqlCommand(insertStr,conn); comm.Parameters.Add(new SqlParameter("@image_data",SqlDbType.Image));//添加参数 comm.Parameters["@image_data"].Value = fileByte;//给参数赋值 comm.Parameters.Add(new SqlParameter("@image_content_type",SqlDbType.VarChar,50)); comm.Parameters["@image_content_type"].Value = upFile.ContentType;//记录图片类型 comm.Parameters.Add(new SqlParameter("@image_description",SqlDbType.VarChar,50)); comm.Parameters["@image_description"].Value = txtDescription.Text;//把其他的表单数据上传 comm.Parameters.Add(new SqlParameter("@image_size",SqlDbType.Int,4)); comm.Parameters["@image_size"].Value = upFile.ContentLength;//记录图片长度,读取数据的时候使用 conn.Open();//打开数据库连接 comm.ExecuteNonQuery();//添加数据 conn.Close();//关闭数据库 txtMessage.Text = "你已经成功的上传了图片"; } } catch(Exception ex) { txtMessage.Text = ex.Message.ToString(); } } }读取using(SqlConnection conn=new SqlConnection()) { conn.ConnectionString=""; string strSql="select * from Tb where Id='"+Id+"'"; SqlCommand cmd=new SqlCommand(strSql,conn) ; conn.Open(); SqlDataReader reader=cmd.ExecuteReader(); if(reader.Read()) { Response.ContentType = "application/octet-stream"; Response.BinaryWrite((Byte[])reader["Photo"]); } Response.End(); conn.Close(); } 在.ascx页面调用js 急…… split函数问题 DEV控件使用脚本。。 System.Web.Mail发送邮件问题 很急的事,截取字符的问题 wenbo234来接分 初来乍到!真诚求教!以前搞asp前后台设计,自己感觉落伍了! 非常奇怪的现象:没有ASPNET用户 高手请指教 form验证的问题,提示配置错误:无法识别的配置节“location”,各位帮忙看看了! Request.QueryString["id"] 取回来的数据,不安全怎么办? 我做的下载总是到99%就卡住了,大家帮我看看!
if (ddlist.SelectedItem.Value == "")
{
ScriptManager.RegisterStartupScript(this, this.GetType(), "", "alert('你还没有选择专栏');window.location.href='User.aspx';", true);
return;
}
else
{
type = ddlist.SelectedItem.Value;
ViewState["type"] = type;
ddlist.Enabled = false;
}
Boolean fileOK = true;
if (fUfile.HasFile)
{
if (fileOK)
{
try
{
string Date = System.DateTime.Now.ToString("yyMMddhhmmss");
string subFie = fUfile.FileName.Substring(fUfile.FileName.LastIndexOf("."), 4);
string sLocalPath = "../common/wwjeditor/UploadFiles/";
string folder = Server.MapPath(sLocalPath) + type; if (!Directory.Exists(folder))
Directory.CreateDirectory(folder);
string path = folder + "\\" + Date + subFie; string fileName = fUfile.FileName;
int size = fUfile.PostedFile.ContentLength;
// string userName = type; Multimedia mul = new Multimedia();
mul.Name = fileName;
mul.Size = size;
mul.Path = "common/wwjeditor/UploadFiles/" + type + "/" + Date + subFie;
mul.Series = 1; //上传到文件夹
sLocalPath = "../common/wwjeditor/UploadFiles/" + type;
path = Server.MapPath(sLocalPath) + "\\" + mul.Path.Substring(mul.Path.LastIndexOf("/") + 1);
if (!File.Exists(path))
{
fUfile.SaveAs(path);
}
if (ViewState["list"] != null)
{
List<Multimedia> list =(List<Multimedia>) ViewState["list"];
mul.Series = list.Count + 1;
list.Add(mul);
gvAccessory.DataSource = list;
gvAccessory.DataBind();
}
else
{
List<Multimedia> list = new List<Multimedia>();
list.Add(mul);
ViewState["list"] = list;
gvAccessory.DataSource = list;
gvAccessory.DataBind();
} }
catch (System.Exception ex)
{
Response.Write("<script>alert('" + ex.Message + "');</script>");
return;
}
}
}
else
{
Response.Write("<script>alert('上传的文件不符合要求!');</script>");
return;
}
if (FileUpLogo.HasFile)
{
//取得上传文件的大小
int FileLen = FileUpLogo.PostedFile.ContentLength;
Byte[] FileData = new Byte[FileLen];
//创建访问客户端上传文件的对象
HttpPostedFile hp = FileUpLogo.PostedFile;
//创建数据流对象
System.IO.Stream sr = hp.InputStream;
//将图片数据放到FileData数组对象实例中,0代表数组指针的起始位置,FileLen代表指针的结束位置
sr.Read(FileData, 0, FileLen);
//将FileData 赋值给实体
brandModel.fld_logo = FileData;
}
或者HttpPostedFile upFile = up_file.PostedFile;//HttpPostedFile对象,用来读取上传图片的属性
fileLength = upFile.ContentLength;//记录文件的长度
try
{
if(fileLength==0)//当文件长度为0的时候
{
txtMessage.Text = "请选择要上传的文件!";
}
else
{
byte[] fileByte = new byte[fileLength];//用图片的长度来初始化一个字节数组存储临时的图片文件
Stream fileStream = upFile.InputStream;//建立文件流对象
fileStream.Read(fileByte,0,fileLength);//读取图片数据到临时存储体fileByte,0为数据指针位置,fileLength为数据长度
string connString = "Data Source=192.168.1.250;database=image;uid=pwqzc;pwd=cn0088";
SqlConnection conn = new SqlConnection(connString);//初始化数据库连接
string insertStr = "insert into image (image_data,image_content_type,image_description,image_size) values (@image_data,@image_content_type,@image_description,@image_size)";
//插入数据库语句
SqlCommand comm = new SqlCommand(insertStr,conn);
comm.Parameters.Add(new SqlParameter("@image_data",SqlDbType.Image));//添加参数
comm.Parameters["@image_data"].Value = fileByte;//给参数赋值
comm.Parameters.Add(new SqlParameter("@image_content_type",SqlDbType.VarChar,50));
comm.Parameters["@image_content_type"].Value = upFile.ContentType;//记录图片类型
comm.Parameters.Add(new SqlParameter("@image_description",SqlDbType.VarChar,50));
comm.Parameters["@image_description"].Value = txtDescription.Text;//把其他的表单数据上传
comm.Parameters.Add(new SqlParameter("@image_size",SqlDbType.Int,4));
comm.Parameters["@image_size"].Value = upFile.ContentLength;//记录图片长度,读取数据的时候使用
conn.Open();//打开数据库连接
comm.ExecuteNonQuery();//添加数据
conn.Close();//关闭数据库
txtMessage.Text = "你已经成功的上传了图片";
}
}
catch(Exception ex)
{
txtMessage.Text = ex.Message.ToString();
}
}
}读取
using(SqlConnection conn=new SqlConnection())
{
conn.ConnectionString="";
string strSql="select * from Tb where Id='"+Id+"'";
SqlCommand cmd=new SqlCommand(strSql,conn) ;
conn.Open();
SqlDataReader reader=cmd.ExecuteReader();
if(reader.Read())
{
Response.ContentType = "application/octet-stream";
Response.BinaryWrite((Byte[])reader["Photo"]);
}
Response.End();
conn.Close();
}