asp.net 小弟我想做个上传图片到数据库的方法
asp.net 我想做个上传图片到数据库的方法
我想在网页中实现上传图片到数据库的功能。我是参考这个网站第二个方法的。http://developer.51cto.com/art/200907/138274.htm
但是现在我按照他的做却出了问题。我有一个FileUpload控件,一个Image控件;一个label;一个上传按钮。代码如下:
using System;
using System.Collections.Generic;
using System.Data;
using System.Data.SqlClient;
using System.IO;
using System.Linq;
using System.Web;
using System.Web.UI;
using System.Web.UI.WebControls;
public partial class 照片管理 : System.Web.UI.Page
{
protected void Page_Load(object sender, EventArgs e)
{
Image1.ImageUrl = "显示图片.aspx";
}
protected void Button1_Click(object sender, EventArgs e)
{
string name = FileUpload1.PostedFile.FileName;
string type = name.Substring(name.LastIndexOf(".") + 1);
FileStream fa = File.OpenRead(name);//问题出在这里,提示我:未能找到文件
byte[] content = new byte[fa.Length];
fa.Read(content, 0, content.Length);
fa.Close();
SqlConnection conn = new SqlConnection("Data Source=PC201503061527;Initial Catalog=talkroom;User ID=sa;Password=sa");
SqlCommand cmd = conn.CreateCommand();
conn.Open();
cmd.CommandText = "insert into MyImage(Image_Content) valua (@content)";
cmd.CommandType = CommandType.Text;
if (type == "jpg" || type == "gif" || type == "bmp" || type == "png")
{
SqlParameter para = cmd.Parameters.Add("@content", SqlDbType.Image);
para.Value = content;
cmd.ExecuteNonQuery();
}
}
}
求告诉哪里错了,怎么改!不胜感激!!!
------解决思路----------------------
你不是大部分都写好了吗,只不过少了个环节而已
不要获取客户端文件的路径,获取了也没用
就自己用时间戳拼接个临时文件名,然后存入服务器本地目录里(必须是网站目录下)
路径这样写string path = Server.MapPath("~/file_upload/" + name);
save完之后从这个路径里再把文件读出来
------解决思路----------------------
然后再就是创建一张表,字段为FileContent,类型为image
我想在网页中实现上传图片到数据库的功能。我是参考这个网站第二个方法的。http://developer.51cto.com/art/200907/138274.htm
但是现在我按照他的做却出了问题。我有一个FileUpload控件,一个Image控件;一个label;一个上传按钮。代码如下:
using System;
using System.Collections.Generic;
using System.Data;
using System.Data.SqlClient;
using System.IO;
using System.Linq;
using System.Web;
using System.Web.UI;
using System.Web.UI.WebControls;
public partial class 照片管理 : System.Web.UI.Page
{
protected void Page_Load(object sender, EventArgs e)
{
Image1.ImageUrl = "显示图片.aspx";
}
protected void Button1_Click(object sender, EventArgs e)
{
string name = FileUpload1.PostedFile.FileName;
string type = name.Substring(name.LastIndexOf(".") + 1);
FileStream fa = File.OpenRead(name);//问题出在这里,提示我:未能找到文件
byte[] content = new byte[fa.Length];
fa.Read(content, 0, content.Length);
fa.Close();
SqlConnection conn = new SqlConnection("Data Source=PC201503061527;Initial Catalog=talkroom;User ID=sa;Password=sa");
SqlCommand cmd = conn.CreateCommand();
conn.Open();
cmd.CommandText = "insert into MyImage(Image_Content) valua (@content)";
cmd.CommandType = CommandType.Text;
if (type == "jpg" || type == "gif" || type == "bmp" || type == "png")
{
SqlParameter para = cmd.Parameters.Add("@content", SqlDbType.Image);
para.Value = content;
cmd.ExecuteNonQuery();
}
}
}
求告诉哪里错了,怎么改!不胜感激!!!
------解决思路----------------------
你不是大部分都写好了吗,只不过少了个环节而已
不要获取客户端文件的路径,获取了也没用
就自己用时间戳拼接个临时文件名,然后存入服务器本地目录里(必须是网站目录下)
路径这样写string path = Server.MapPath("~/file_upload/" + name);
save完之后从这个路径里再把文件读出来
------解决思路----------------------
public static string ConvertBytesToString(byte[] bytes)
{
StringBuilder sb = new StringBuilder();
sb.Append("0x");
foreach (byte b in bytes)
{
string sby = Convert.ToString(b, 16);
if (sby.Length == 1)
{
sby = "0" + sby;
}
sb.Append(sby);
}
return sb.ToString();
}
然后再就是创建一张表,字段为FileContent,类型为image
string sql = string.Format("Insert Into 表 (FileContent) Values ('{0}')",ConvertBytesToString(文件字节));
ExecuteSql(sql);.................