If File1.PostedFile.ContentLength > 0 Then sFileName = pf.GetFileName(File1.PostedFile.FileName) sFileType = File1.PostedFile.ContentType sFilePath = ".." '指定存在..目录 If Left(sFileType, 5) <> "image" Then '所要上传的文件不是图片,请重新选取! End If File1.PostedFile.SaveAs(sFilePath) End IfResponse.ContentType = "image/*" Response.BinaryWrite(dt.Rows(0)("FileCont"))
二进制存进数据库If FileUpload1.HasFile Then Dim FileExt As String = System.IO.Path.GetExtension(FileUpload1.FileName) Dim ContentType As String = FileUpload1.PostedFile.ContentType Dim fileData As Byte() = FileUpload1.FileBytes Dim sql As String sql = "INSERT INTO UserList (Title,ContentType,Photo) Values(@Title,@ContentType,@Photo)" Dim ConnectionString As String = "Data Source=localhost;Initial Catalog=aaa;Integrated Security=True" Dim cn As SqlConnection = New SqlConnection(ConnectionString) cn.Open() Dim cmd As SqlCommand = New SqlCommand(sql, cn) cmd.Parameters.Add("@Title", System.Data.SqlDbType.NVarChar) cmd.Parameters.Add("@ContentType", System.Data.SqlDbType.VarChar) cmd.Parameters.Add("@Photo", System.Data.SqlDbType.Image) cmd.Parameters("@Title").Value = TextBox1.Text cmd.Parameters("@ContentType").Value = ContentType cmd.Parameters("@Photo").Value = fileData cmd.ExecuteNonQuery() cn.Close() cn.Dispose() End If取图 Response.BinaryWrite(CType(myDataReader("Photo"), Byte()))
private void WriteImage()
{
SqlCommand comm = conn.CreateCommand();
comm.CommandText = "insert into images(image,type) values(@image,@type)";
comm.CommandType = CommandType.Text;
SqlParameter param = comm.Parameters.Add("@image",SqlDbType.Image);
param.Value = ReadFile();
param = comm.Parameters.Add("@type",SqlDbType.NVarChar);
param.Value = GetContentType(new FileInfo(fileName).Extension.Remove(0,1)); if(comm.ExecuteNonQuery() == 1)
Response.Write("Successful");
else
Response.Write("Fail");
conn.Close();
} //获取图片的后缀名
private string GetContentType(string extension)
{
string type = ""; if(extension.Equals("jpg") || extension.Equals("JPG"))
type = "jpeg";
else
type = extension; return "image/"+type;
} //从数据库中读取图片
private void ReadImage()
{
SqlCommand comm = conn.CreateCommand();
comm.CommandText = "select image,type from images";
comm.CommandType = CommandType.Text; SqlDataReader reader = comm.ExecuteReader();
while(reader.Read())
{
Response.ContentType = reader["type"].ToString();//读写类型 一定要设置 否则浏览器会当作文本输出
Response.BinaryWrite((byte[])reader["image"]);//图片数据
} Response.Write("Successful");
Response.End(); conn.Close();
}本文来自CSDN博客,转载请标明出处:http://blog.csdn.net/21aspnet/archive/2004/10/26/153202.aspx
private string GetFile()
{
HttpPostedFile file = File1.PostedFile;
fileName = file.FileName; return fileName;
} //读取文件内容
private byte[] ReadFile()
{
FileStream file = File.OpenRead(GetFile());
byte[] content = new byte[file.Length];
file.Read(content,0,content.Length);
file.Close();
return content;
} //连接数据库
private void ConnectDB()
{
string connStr = "Initial Catalog=;Data Source=;User ID=;Password=;";
conn = new SqlConnection(connStr);
conn.Open();
}
If File1.PostedFile.ContentLength > 0 Then sFileName = pf.GetFileName(File1.PostedFile.FileName)
sFileType = File1.PostedFile.ContentType
sFilePath = ".." '指定存在..目录 If Left(sFileType, 5) <> "image" Then
'所要上传的文件不是图片,请重新选取!
End If File1.PostedFile.SaveAs(sFilePath)
End IfResponse.ContentType = "image/*"
Response.BinaryWrite(dt.Rows(0)("FileCont"))
string picturename = FileUpload1.FileName;
string lastname = picturename.Substring(picturename.LastIndexOf(".") + 1);
if (lastname.ToLower() == "bmp" || lastname.ToLower() == "jpg" || lastname.ToLower() == "gif")
{
try
{
SqlConnection conn = new SqlConnection();
conn.ConnectionString = "Data Source=localhost;Initial Catalog=zcpd;Integrated Security=True";
conn.Open();
int len = FileUpload1.PostedFile.ContentLength;
Byte[] picturedata = new Byte[len];
HttpPostedFile htp = FileUpload1.PostedFile;
Stream srm = htp.InputStream;
srm.Read(picturedata, 0, len);
SqlCommand cmd = new SqlCommand();
cmd.CommandText = "";
cmd.Connection = conn;
cmd.Parameters.Add("@picturedata", SqlDbType.Image);
cmd.Parameters["@picturedata"].Value = picturedata;
int affect = cmd.ExecuteNonQuery();
conn.Close();
conn.Dispose();
if (affect == 1)
lblMessage.Text = "上报成功!";
else
lblMessage.Text = "上报失败!";
}
catch (SqlException ex)
{
lblMessage.Text = "出错";
}
}
else
lblMessage.Text = "上传照片文件格式不对!";
取:
SqlConnection conn = new SqlConnection();
conn.ConnectionString = "Data Source=localhost;Initial Catalog=zcpd;Integrated Security=True";
conn.Open();
SqlCommand cmd = new SqlCommand();
cmd.CommandText = "";
cmd.Connection = conn;
SqlDataReader sdr = cmd.ExecuteReader();
sdr.Read();
Response.BinaryWrite((Byte[])sdr["zhaopian"]);
sdr.Close();
conn.Close();
conn.Dispose();
使用的是IO的几个stream类
楼上的方法也没有问题
Dim FileExt As String = System.IO.Path.GetExtension(FileUpload1.FileName)
Dim ContentType As String = FileUpload1.PostedFile.ContentType
Dim fileData As Byte() = FileUpload1.FileBytes
Dim sql As String
sql = "INSERT INTO UserList (Title,ContentType,Photo) Values(@Title,@ContentType,@Photo)"
Dim ConnectionString As String = "Data Source=localhost;Initial Catalog=aaa;Integrated Security=True"
Dim cn As SqlConnection = New SqlConnection(ConnectionString)
cn.Open()
Dim cmd As SqlCommand = New SqlCommand(sql, cn)
cmd.Parameters.Add("@Title", System.Data.SqlDbType.NVarChar)
cmd.Parameters.Add("@ContentType", System.Data.SqlDbType.VarChar)
cmd.Parameters.Add("@Photo", System.Data.SqlDbType.Image)
cmd.Parameters("@Title").Value = TextBox1.Text
cmd.Parameters("@ContentType").Value = ContentType
cmd.Parameters("@Photo").Value = fileData
cmd.ExecuteNonQuery()
cn.Close()
cn.Dispose()
End If取图 Response.BinaryWrite(CType(myDataReader("Photo"), Byte()))
using (SqlConnection conn = new SqlConnection(sqlconnstr))
{
conn.Open();
SqlCommand cmd = new SqlCommand();
cmd.Connection = conn;
cmd.CommandText = "select * from Tb_img";
SqlDataReader sdr = cmd.ExecuteReader();
sdr.Read();
MyData = (byte[])sdr["ImgFile"];
Response.ContentType = "image/gif";
Response.BinaryWrite(MyData);
conn.Close();
}HttpPostedFile UpFile = UP_File.PostedFile;
FileLength = UpFile.ContentLength;
Byte[] FileByteArray = new byte[FileLength];
Stream StreamObj = UpFile.InputStream;
StreamObj.Read(FileByteArray, 0, FileLength);
using (SqlConnection conn = new SqlConnection(sqlconnstr))
{
String SqlCmd = "INSERT INTO Tb_Img(imgFile,ImageContentType,ImageDescription,ImageSize) VALUES (@Image,@ContentType,@ImageDescription,@ImageSize)";
SqlCommand Cmd = new SqlCommand(SqlCmd, Con);
Cmd.Parameters.Add("@ImgFile", SqlDbType.Binary, FileLength).Value = FileByteArray;
Cmd.Parameters.Add("@ContentType", SqlDbType.VarChar, 50).Value = UpFile.ContentType;
Cmd.Parameters.Add("@ImageDescription", SqlDbType.VarChar, 200).Value = txtDescription.Text;
Cmd.Parameters.Add("@ImageSize", SqlDbType.BigInt, 8).Value = UpFile.ContentLength;
Cmd.ExecuteNonQuery();
}