HttpPostedFile UpFile = UP_FILE.PostedFile; //HttpPostedFile对象,用于读取图象文件属性
int FileLength = UpFile.ContentLength; //记录文件长度
try
{
if (FileLength == 0)
{ //文件长度为零时
txtMessage.Text = "<b>请你选择你要上传的文件</b>";
}
else
{
Byte[] FileByteArray = new Byte[FileLength]; //图象文件临时储存Byte数组
Stream StreamObject = UpFile.InputStream; //建立数据流对像
//读取图象文件数据,FileByteArray为数据储存体,0为数据指针位置、FileLnegth为数据长度
StreamObject.Read(FileByteArray,FileLength);
//建立sql Server链接
sqlConnection Con = new sqlConnection("Data Source=YFXIANGGX;Initial Catalog=jsmstc_teach;User ID=sa;Pwd=1;");
//type 上传文件类型,length上传文件长度,info image,info1 NVarChar,info2 VarChar,info3 VarBinary
String sqlCmd = "INSERT INTO BigField (type,length,info,info1,info2,info3) VALUES (@type,@length,@info,@info1,@info2,@info3)";
sqlCommand Cmdobj = new sqlCommand(sqlCmd,Con);
Cmdobj.Parameters.Add("@type",sqlDbType.Char,50).Value = UpFile.ContentType;
Cmdobj.Parameters.Add("@length",sqlDbType.Int).Value = UpFile.ContentLength;
Cmdobj.Parameters.Add("@info",sqlDbType.Image).Value = FileByteArray;
Cmdobj.Parameters.Add("@info1",sqlDbType.NVarChar).Value = FileByteArray.ToString();
Cmdobj.Parameters.Add("@info2",sqlDbType.VarChar).Value = FileByteArray.ToString();
Cmdobj.Parameters.Add("@info3",sqlDbType.VarBinary,FileLength).Value = FileByteArray;
Con.open();
Cmdobj.ExecuteNonQuery();
Con.Close();
txtMessage.Text = "<p><b>OK!你已经成功上传你的图片</b>";//提示上传成功
}
}
catch (Exception ex)
{
txtMessage.Text = ex.Message.ToString();
}
页面代码:
<%@ Page Language="C#" AutoEventWireup="true" CodeFile="saveall.aspx.cs" Inherits="saveall" %>
<!DOCTYPE html PUBLIC "-//W3C//DTD XHTML 1.0 Transitional//EN" "http://www.w3.org/TR/xhtml1/DTD/xhtml1-transitional.dtd">
<html xmlns="http://www.w3.org/1999/xhtml" >
<head runat="server">
<title>无标题页</title>
</head>
<body>
<h1>测试数据库对于大字段()的存取</h1>
<form id="form1" runat="server">
<div>
<table style="width: 488px">
<tr>
<td>
上传图片(选择你要上传的图片)</</td>
<td>
<asp:FileUpload ID="UP_FILE" runat="server" /></td>
<td>
</td>
</tr>
<tr>
<td style="height: 40px">
文件说明(添加上传图片说明,如:作者、出处)</td>
<td style="height: 40px">
<asp:TextBox ID="TextBox1" runat="server"></asp:TextBox></td>
</tr>
<tr>
<td>
<asp:Button ID="Button1" runat="server" OnClick="Button1_Click" Text="uploadImage" /></td>
<td>
<asp:Label ID="txtMessage" runat="server" Text="Label"></asp:Label></td>
</tr>
</table>
</div>
</form>
</body>
</html>
从数据库中读出时遇到了问题,首先是如何将二进制的数据读出后显示到image上,其次是对于保存成varchar,nvarchar字段的值无法取出来了.都没有解决.读出的数据只能通过response输出到当前页面,无法直接在image控件上输出.保存为image,varbinary字段的值取出来的代码如下:
string ImgID = "1"; //ImgID为图片ID
//建立数据库链接
sqlConnection Con = new sqlConnection("Data Source=YFXIANGGX;Initial Catalog=jsmstc_teach;User ID=sa;Pwd=1;");
String sqlCmd = "SELECT * FROM BigField WHERE id = @id";
sqlCommand Cmdobj = new sqlCommand(sqlCmd,Con);
Cmdobj.Parameters.Add("@id",sqlDbType.Int).Value = ImgID;
Con.open();
sqlDataReader sqlReader = Cmdobj.ExecuteReader();
sqlReader.Read();
//Response.ContentType = (string)sqlReader["ImageContentType"];//设定输出文件类型
Response.ContentType = sqlReader["type"].ToString();//设定输出文件类型
//输出图象文件二进制数制
Response.OutputStream.Write((byte[])sqlReader["info"],(int)sqlReader["length"]);//或者是sqlReader["info3"],
Response.End();
Con.Close();
版权声明:本文内容由互联网用户自发贡献,该文观点与技术仅代表作者本人。本站仅提供信息存储空间服务,不拥有所有权,不承担相关法律责任。如发现本站有涉嫌侵权/违法违规的内容, 请发送邮件至 [email protected] 举报,一经查实,本站将立刻删除。