当然是在数据库中读取及显示在Web页中啦,请看以下程序: //ReadImage.aspx程序内容如下: /----------------------------------------------------------------------- <%@ Page Inherits="ReadImage.MainDisplay" SRC="ReadImage.cs"%> //---------------------------------------------------------------------- //ReadImage.cs程序内容如下: using System; using System.Data; using System.Data.SqlClient; using System.Web.UI; using System.Web.UI.WebControls; using System.Web.UI.HtmlControls; namespace ReadImage { public class MainDisplay : System.Web.UI.Page { public void Page_Load(System.Object sender, System.EventArgs e) {
int ImgID = Convert.ToInt32(Request.QueryString["ImgID"]); //ImgID为图片ID //建立数据库链接 SqlConnection Con = new SqlConnection("Data Source=KING;Initial Catalog=testdb;User ID=sa;Pwd=;"); String SqlCmd = "SELECT * FROM ImageStore WHERE ImageID = @ImageID"; SqlCommand CmdObj = new SqlCommand(SqlCmd, Con); CmdObj.Parameters.Add("@ImageID", SqlDbType.Int).value = ImgID; Con.Open(); SqlDataReader SqlReader = CmdObj.ExecuteReader(); SqlReader.Read(); Response.ContentType = (string)SqlReader["ImageContentType"];//设定输出文件类型 //输出图象文件二进制数制 Response.OutputStream.Write((byte[])SqlReader["ImageData"], 0, (int)SqlReader["ImageSize"]); Response.End(); Con.Close(); //很简单吧^_^ } } }
Dim FileByteArrayA() As Byte Dim StreamObject As Stream Dim splitStringA() As String splitStringA = Request.Files("txfFile1").FileName.Split("\\") ReDim FileByteArrayA(Request.Files("txfFile1").ContentLength) StreamObject = Request.Files("txfFile1").InputStream StreamObject.Read(FileByteArrayA, 0, Request.Files("txfFile1").ContentLength)然后你想把splitStringA怎么样就怎么样了,update() set nn=splitStringA 不过上面用到了HTML中的一个控件的file field
O_DraftArchData data = new O_DraftArchData();DataRow dr = data.Tables[0].NewRow(); data.Tables[0].Rows.Add(dr); if (txtFileContents.PostedFile != null) { mFilename=getRealFileName(txtFileContents.PostedFile.FileName); if (mFilename!="") { intDocLen = txtFileContents.PostedFile.ContentLength; byte[] Docbuffer = new byte[intDocLen]; objStream = txtFileContents.PostedFile.InputStream; objStream.Read(Docbuffer ,0,intDocLen); dr["MyImg"] = Docbuffer;//图像 dr["MyImgName"] = mFilename;//图像文件名 } } UpdateData(data) //更新数据库
Function SaveToDB(ByVal imgbin As Byte(), ByVal type As String) As Integer Dim connection As New SqlConnection(cn.ConnStr) Dim command = New SqlCommand("INSERT INTO zx0237(makformid,imgData,modDate,moduser) VALUES ( @img_name, @img_data,@type,@user)", connection) Dim param0 = New SqlParameter("@img_name", SqlDbType.VarChar, 50) param0.Value = Trim(theValue.Value) command.Parameters.Add(param0) Dim param1 = New SqlParameter("@img_data", SqlDbType.Image) param1.Value = imgbin command.Parameters.Add(param1) Dim param2 = New SqlParameter("@type", SqlDbType.VarChar) param2.Value = type command.Parameters.Add(param2) Dim param3 = New SqlParameter("@user", SqlDbType.VarChar) param3.Value = Session("StuffID") & "" command.Parameters.Add(param3) connection.Open() Dim numRowsAffected As Integer = command.ExecuteNonQuery() connection.Close() SaveToDB = numRowsAffected End Function Private Sub Button1_ServerClick(ByVal sender As System.Object, ByVal e As System.EventArgs) Handles Button1.ServerClick Dim imgLen As Integer Dim Co As New ClassControlOpration() If Trim(theValue.Value) = "" Then Response.Write("<script>alert('请选择一张报价单号!');</script>") Exit Sub End If imgLen = File1.PostedFile.ContentLength Dim imgContentType As String = File1.PostedFile.FileName Dim imgName As String = imgContentType Dim imgBinaryData As Byte() ReDim imgBinaryData(imgLen) Dim n As Integer = File1.PostedFile.InputStream.Read(imgBinaryData, 0, imgLen) Try Dim RowsAffected As Integer = SaveToDB(imgBinaryData, DateTime.Now) If (RowsAffected > 0) Then DIV1.InnerText = "保存成功!" Else Co.Alert(Page, "保存记录出错了" & Err.Description) Co = Nothing End If imgBinaryData = Nothing Catch If Err.Number = 5 Then DIV1.InnerText = "该单的图片已上传!" End If End Try End Sub
litp(天道酬勤)
我按照你的方法去做了 可是老是说这个错误是怎么回事呀?“ C:\Documents and Settings\Administrator\VSWebCache\XIA-09EA91A7C0D\UpLoadImage\UpLoadImage.aspx.cs(47): “System.Data.SqlClient.SqlParameter”并不包含对“value”的定义”
litp(天道酬勤)
我按照你的方法去做了 可是老是说这个错误是怎么回事呀?“ C:\Documents and Settings\Administrator\VSWebCache\XIA-09EA91A7C0D\UpLoadImage\UpLoadImage.aspx.cs(47): “System.Data.SqlClient.SqlParameter”并不包含对“value”的定义”
http://dotnet.aspx.cc/ShowDetail.aspx?id=2A5DD7C6-A45A-48AB-A2E8-342A29F17506
<%@ Import Namespace="System.IO" %>
<%@ Import Namespace="System.Drawing" %>
<%@ Import Namespace="System.Drawing.Imaging" %>
<html>
<script runat =server>
void UploadBtn_Click(Object sender, EventArgs e) {
String filename;
String filename1;
String[] filename2;
int q;
filename=UploadFile.PostedFile.FileName ;
filename2=filename.Split(new Char[] {'\\'});
q=filename2.GetUpperBound(0);
filename1=filename2[q];
dis.Text="上传文件名:"+filename1+"<br/>";
UploadFile.PostedFile.SaveAs(Server.MapPath(filename1));
ImageEditor.Visible = true;
dis.Text+="文件大小:"+UploadFile.PostedFile.ContentLength+"字节数";
Image1.Src=filename1;
}
void UpdateBtn_Click(Object sender, EventArgs e) {
String filename1;
filename1=Image1.Src;
System.Drawing.Image image = System.Drawing.Image.FromFile(Server.MapPath(filename1));
System.Drawing.Image newimage = new Bitmap(image.Width, image.Height, PixelFormat.Format32bppRgb);
Graphics g = Graphics.FromImage(newimage);
g.DrawImage(image,0,0,image.Width,image.Height);
Font f = new Font(FontType.SelectedItem.Text, Int32.Parse(FontSize.SelectedItem.Text));
Brush b = new SolidBrush(Color.Red);
g.DrawString(Caption.Text, f, b, 10, 140);
g.Dispose();
//System.Drawing.Image thumbImage = newimage.GetThumbnailImage(Convert.ToInt32(Width.Text),Convert.ToInt32(Height.Text),null,0);
image.Dispose();
// thumbImage.Save(Server.MapPath(filename1), ImageFormat.Jpeg);
Image1.Src=filename1;
Caption.Text="";
} </script> <body>
<asp:label id="dis" runat=server/>
<form enctype="multipart/form-data" runat=server> <h3> Select File To Upload: <input id="UploadFile" type=file runat=server> <asp:button Text="Upload Me!" OnClick="UploadBtn_Click" runat=server/> <hr>
<asp:panel id="ImageEditor" Visible=false runat=server> <img ID="Image1" src="" runat="server"/> <h3>
Image Width: <asp:textbox id="Width" runat=server/> Image Height: <asp:textbox id="Height" runat=server/> <br/> Text Caption: <asp:textbox id="Caption" runat=server/> Caption Size: <asp:dropdownlist id="FontSize" runat=server>
<asp:listitem>14</asp:listitem>
<asp:listitem>18</asp:listitem>
<asp:listitem>26</asp:listitem>
<asp:listitem>36</asp:listitem>
<asp:listitem>48</asp:listitem>
<asp:listitem>62</asp:listitem>
</asp:dropdownlist>
Caption Font: <asp:dropdownlist id="FontType" runat=server>
<asp:listitem>黑体</asp:listitem>
<asp:listitem>仿宋</asp:listitem>
<asp:listitem>隶书</asp:listitem>
<asp:listitem>楷书</asp:listitem>
<asp:listitem>方正姚体</asp:listitem>
<asp:listitem>华文彩云</asp:listitem>
</asp:dropdownlist>
<asp:button Text="Update Image" OnClick="UpdateBtn_Click" runat=server/> </h3>
</asp:panel> </form> </body>
</html>
//开发环境:Window 2000、SQLServer2000、.Net Framework SDK正式版
//开发语言:C#、ASP.Net
//简介:数据库中图片存蓄及读取
/*
说明:在ASP中,我们用Request.TotalBytes、Request.BinaryRead()来上传图片,这个可恶的BinaryRead()方法非常笨,单个文件上传倒没什么大事,单如果多个图片上专可就花大气力了…!而现在ASP.Net中将会把解决以前ASP中文件上传的种种问题,使你在ASP.Net中轻轻松松开发出功能强大的上传程序,下面大家看看例子啦。
*/
/*
首先在SQL Server中建立一个图片存储的数库表,ImageData Column为图象二进制数据储存字段,ImageContentType Column为图象文件类型记录字段,ImageDescription Column为储蓄图象文件说明字段,ImageSize Column为储存图象文件长度字段,结构如下:
CREATE TABLE [dbo].[ImageStore] (
[ImageID] [int] IDENTITY (1, 1) NOT NULL ,
[ImageData] [image] NULL ,
[ImageContentType] [varchar] (50) COLLATE Chinese_PRC_CI_AS NULL ,
[ImageDescription] [varchar] (200) COLLATE Chinese_PRC_CI_AS NULL ,
[ImageSize] [int] NULL
) ON [PRIMARY] TEXTIMAGE_ON [PRIMARY]
*/
//UpLoadImage.aspx程序内容如下: <%@ Page Inherits="UploadImage.UploadImage" SRC="UpLoadImage.cs" Language="C#"%>
<HTML><title>上传图片</title>
<BODY bgcolor="#FFFFFF">
<FORM ENCTYPE="multipart/form-data" RUNAT="server" ID="Form1">
<TABLE RUNAT="server" WIDTH="700" ALIGN="left" ID="Table1" cellpadding="0" cellspacing="0" border="0">
<TR>
<TD>上传图片(选择你要上传的图片)</TD>
<TD>
<INPUT TYPE="file" ID="UP_FILE" RUNAT="server" STYLE="Width:320" ACCEPT="text/*" NAME="UP_FILE">
</TD>
</TR>
<TR>
<TD>
文件说明(添加上传图片说明,如:作者、出处)
</TD>
<TD>
<asp:TextBox RUNAT="server" WIDTH="239" ID="txtDescription" MAINTAINSTATE="false" />
</TD>
</TR>
<TR>
<TD>
<asp:Label RUNAT="server" ID="txtMessage" FORECOLOR="red" MAINTAINSTATE="false" />
</TD>
<TD>
<asp:Button RUNAT="server" WIDTH="239" onCLICK="Button_Submit" TEXT="Upload Image" />
</TD>
</TR>
</TABLE>
</FORM>
</BODY>
</HTML> //-------------------------------------------------------------------
//UpLoadImage.cs程序内容如下: using System;
using System.Web;
using System.IO;
using System.Data;
using System.Data.SqlClient;
using System.Web.UI;
using System.Web.UI.WebControls;
using System.Web.UI.HtmlControls;
namespace UploadImage
{
public class UploadImage : Page
{
protected HtmlInputFile UP_FILE; //HtmlControl、WebControls控件对象
protected TextBox txtDescription;
protected Label txtMessage;
protected Int32 FileLength = 0; //记录文件长度变量
protected void Button_Submit(System.Object sender, System.EventArgs e)
{
HttpPostedFile UpFile = UP_FILE.PostedFile; //HttpPostedFile对象,用于读取图象文件属性
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,0,FileLength);
//建立SQL Server链接
SqlConnection Con = new SqlConnection("Data Source=Localhost;Initial Catalog=testdb;User ID=sa;Pwd=;");
String SqlCmd = "INSERT INTO ImageStore (ImageData, ImageContentType, ImageDescription, ImageSize) valueS (@Image, @ContentType, @ImageDescription, @ImageSize)";
SqlCommand CmdObj = new SqlCommand(SqlCmd, Con);
CmdObj.Parameters.Add("@Image",SqlDbType.Binary, FileLength).value = FileByteArray;
CmdObj.Parameters.Add("@ContentType", SqlDbType.VarChar,50).value = UpFile.ContentType; //记录文件类型
//把其它单表数据记录上传
CmdObj.Parameters.Add("@ImageDescription", SqlDbType.VarChar,200).value = txtDescription.Text;
//记录文件长度,读取时使用
CmdObj.Parameters.Add("@ImageSize", SqlDbType.BigInt,8).value = UpFile.ContentLength;
Con.Open();
CmdObj.ExecuteNonQuery();
Con.Close();
txtMessage.Text = "<p><b>OK!你已经成功上传你的图片</b>";//提示上传成功
}
}
catch (Exception ex)
{
txtMessage.Text = ex.Message.ToString();
}
}
}
}
//----------------------------------------------------------------------
//好了,图片已经上传到数据库,现在还要干什么呢?
当然是在数据库中读取及显示在Web页中啦,请看以下程序:
//ReadImage.aspx程序内容如下: /-----------------------------------------------------------------------
<%@ Page Inherits="ReadImage.MainDisplay" SRC="ReadImage.cs"%>
//---------------------------------------------------------------------- //ReadImage.cs程序内容如下:
using System;
using System.Data;
using System.Data.SqlClient;
using System.Web.UI;
using System.Web.UI.WebControls;
using System.Web.UI.HtmlControls;
namespace ReadImage {
public class MainDisplay : System.Web.UI.Page {
public void Page_Load(System.Object sender, System.EventArgs e) {
int ImgID = Convert.ToInt32(Request.QueryString["ImgID"]); //ImgID为图片ID //建立数据库链接
SqlConnection Con = new SqlConnection("Data Source=KING;Initial Catalog=testdb;User ID=sa;Pwd=;");
String SqlCmd = "SELECT * FROM ImageStore WHERE ImageID = @ImageID";
SqlCommand CmdObj = new SqlCommand(SqlCmd, Con);
CmdObj.Parameters.Add("@ImageID", SqlDbType.Int).value = ImgID;
Con.Open();
SqlDataReader SqlReader = CmdObj.ExecuteReader();
SqlReader.Read();
Response.ContentType = (string)SqlReader["ImageContentType"];//设定输出文件类型
//输出图象文件二进制数制
Response.OutputStream.Write((byte[])SqlReader["ImageData"], 0, (int)SqlReader["ImageSize"]);
Response.End();
Con.Close();
//很简单吧^_^
}
}
}
//--------------------------------------------------------------------
//最后,我们当然要把它在Web页面显示出来啦
//ShowImage.hml <html>
<body>
这个是从数据库读取出来的图象:<img src="ReadImage.aspx?ImgID=1">
<body>
</html>
//最后,这程序当然还很多改进之处,希望大家多想想多编编一定可以写出更多的图象上传程序
Dim StreamObject As Stream
Dim splitStringA() As String splitStringA = Request.Files("txfFile1").FileName.Split("\\")
ReDim FileByteArrayA(Request.Files("txfFile1").ContentLength)
StreamObject = Request.Files("txfFile1").InputStream
StreamObject.Read(FileByteArrayA, 0, Request.Files("txfFile1").ContentLength)然后你想把splitStringA怎么样就怎么样了,update() set nn=splitStringA
不过上面用到了HTML中的一个控件的file field
data.Tables[0].Rows.Add(dr);
if (txtFileContents.PostedFile != null)
{
mFilename=getRealFileName(txtFileContents.PostedFile.FileName);
if (mFilename!="")
{
intDocLen = txtFileContents.PostedFile.ContentLength;
byte[] Docbuffer = new byte[intDocLen];
objStream = txtFileContents.PostedFile.InputStream;
objStream.Read(Docbuffer ,0,intDocLen);
dr["MyImg"] = Docbuffer;//图像
dr["MyImgName"] = mFilename;//图像文件名
}
}
UpdateData(data) //更新数据库
Dim connection As New SqlConnection(cn.ConnStr)
Dim command = New SqlCommand("INSERT INTO zx0237(makformid,imgData,modDate,moduser) VALUES ( @img_name, @img_data,@type,@user)", connection)
Dim param0 = New SqlParameter("@img_name", SqlDbType.VarChar, 50)
param0.Value = Trim(theValue.Value)
command.Parameters.Add(param0) Dim param1 = New SqlParameter("@img_data", SqlDbType.Image)
param1.Value = imgbin
command.Parameters.Add(param1) Dim param2 = New SqlParameter("@type", SqlDbType.VarChar)
param2.Value = type
command.Parameters.Add(param2) Dim param3 = New SqlParameter("@user", SqlDbType.VarChar)
param3.Value = Session("StuffID") & ""
command.Parameters.Add(param3)
connection.Open()
Dim numRowsAffected As Integer = command.ExecuteNonQuery()
connection.Close()
SaveToDB = numRowsAffected
End Function
Private Sub Button1_ServerClick(ByVal sender As System.Object, ByVal e As System.EventArgs) Handles Button1.ServerClick
Dim imgLen As Integer
Dim Co As New ClassControlOpration()
If Trim(theValue.Value) = "" Then
Response.Write("<script>alert('请选择一张报价单号!');</script>")
Exit Sub
End If
imgLen = File1.PostedFile.ContentLength
Dim imgContentType As String = File1.PostedFile.FileName
Dim imgName As String = imgContentType
Dim imgBinaryData As Byte()
ReDim imgBinaryData(imgLen)
Dim n As Integer = File1.PostedFile.InputStream.Read(imgBinaryData, 0, imgLen)
Try
Dim RowsAffected As Integer = SaveToDB(imgBinaryData, DateTime.Now)
If (RowsAffected > 0) Then
DIV1.InnerText = "保存成功!"
Else Co.Alert(Page, "保存记录出错了" & Err.Description)
Co = Nothing
End If
imgBinaryData = Nothing
Catch
If Err.Number = 5 Then
DIV1.InnerText = "该单的图片已上传!"
End If
End Try End Sub
我按照你的方法去做了 可是老是说这个错误是怎么回事呀?“
C:\Documents and Settings\Administrator\VSWebCache\XIA-09EA91A7C0D\UpLoadImage\UpLoadImage.aspx.cs(47): “System.Data.SqlClient.SqlParameter”并不包含对“value”的定义”
我按照你的方法去做了 可是老是说这个错误是怎么回事呀?“
C:\Documents and Settings\Administrator\VSWebCache\XIA-09EA91A7C0D\UpLoadImage\UpLoadImage.aspx.cs(47): “System.Data.SqlClient.SqlParameter”并不包含对“value”的定义”