当然可以的:
form1.aspx
using System;
using System.Collections;
using System.ComponentModel;
using System.Data;
using System.Drawing;
using System.Web;
using System.Web.SessionState;
using System.Web.UI;
using System.Web.UI.WebControls;
using System.Web.UI.HtmlControls;
using System.IO;
using System.Data.SqlClient;
namespace uploadFile
{
/// <summary>
/// Form1 的摘要说明。
/// </summary>
public class Form1 : System.Web.UI.Page
{
protected System.Web.UI.WebControls.Button Button1;
protected System.Web.UI.HtmlControls.HtmlInputText imgName;
protected System.Web.UI.WebControls.Button Button2;
protected System.Web.UI.HtmlControls.HtmlInputFile UploadFile;
private void Page_Load(object sender, System.EventArgs e)
{
// 在此处放置用户代码以初始化页面
}
#region Web Form Designer generated code
override protected void OnInit(EventArgs e)
{
//
// CODEGEN:该调用是 ASP.NET Web 窗体设计器所必需的。
//
InitializeComponent();
base.OnInit(e);
}
/// <summary>
/// 设计器支持所需的方法 - 不要使用代码编辑器修改
/// 此方法的内容。
/// </summary>
private void InitializeComponent()
{
this.Button1.Click += new System.EventHandler(this.Button1_Click);
this.Button2.Click += new System.EventHandler(this.Button2_Click);
this.Load += new System.EventHandler(this.Page_Load);
}
#endregion
private void Button1_Click(object sender, System.EventArgs e)
{
Stream imgStream;
int imgLen;
string imgName_value;
string imgContentType;
string imgUploadedName;
imgStream = UploadFile.PostedFile.InputStream;
imgLen = UploadFile.PostedFile.ContentLength;
imgUploadedName = UploadFile.PostedFile.FileName;
byte[] imgBinaryData=new byte[imgLen];
imgContentType = UploadFile.PostedFile.ContentType;
imgName_value = imgName.Value;
try
{
if(imgName_value.Length < 1)
{
imgName_value = GetLastRightOf("//",imgUploadedName );
}
}
catch(Exception myEx)
{
Response.Write(myEx.Message);
}
int n = imgStream.Read(imgBinaryData, 0, imgLen);
int NumRowsAffected = MyDatabaseMethod(imgName_value, imgBinaryData, imgContentType);
if(NumRowsAffected > 0)
Response.Write( "<BR> uploaded image " );
else
Response.Write ( "<BR> an error occurred uploading the image.d " );
}
public string GetLastRightOf(string LookFor,string myString)
{
int StrPos;
StrPos = myString.LastIndexOf(LookFor);
return myString.Substring(StrPos + 1);
}
public int MyDatabaseMethod(string imgName,byte[] imgbin,string imgcontenttype)
{
string Connstr="Data Source=localhost;
uid=sa;
pwd=;
Initial Catalog=companynet";
SqlConnection connection=new SqlConnection(Connstr);
string SQL="INSERT INTO Imagelist (img_name,img_data,img_contenttype) VALUES ( @img_name, @img_data,@img_contenttype )";
SqlCommand command=new SqlCommand ( SQL,connection );
SqlParameter param0=new SqlParameter ( "@img_name", SqlDbType.VarChar,50 );
param0.Value = imgName;
command.Parameters.Add( param0 );
SqlParameter param1=new SqlParameter ( "@img_data", SqlDbType.Image );
param1.Value = imgbin;
command.Parameters.Add( param1 );
SqlParameter param2 =new SqlParameter ( "@img_contenttype", SqlDbType.VarChar,50 );
param2.Value = imgcontenttype;
command.Parameters.Add( param2 );
connection.Open();
int numRowsAffected = command.ExecuteNonQuery();
connection.Close();
return numRowsAffected;
}
private void Button2_Click(object sender, System.EventArgs e)
{
Response.Write("<table align=center width=50 border=1>");
for(int i=14;i<19;i++)
{
Response.Write("<tr><td>");
Response.Write("<img src=form2.aspx?id=" + i +">");
Response.Write("</td><td>zhuhongbo</td>");
Response.Write("</td></tr>");
}
Response.Write("</table>");
}
}
}
form2.aspx:
using System;
using System.Collections;
using System.ComponentModel;
using System.Data;
using System.Drawing;
using System.Web;
using System.Web.SessionState;
using System.Web.UI;
using System.Web.UI.WebControls;
using System.Web.UI.HtmlControls;
using System.IO;
using System.Data.SqlClient;
namespace uploadFile
{
/// <summary>
/// Form2 的摘要说明。
/// </summary>
public class Form2 : System.Web.UI.Page
{
protected System.Web.UI.WebControls.Button Button1;
private void InitializeComponent()
{
this.Load += new System.EventHandler(this.Page_Load);
}
private void Page_Load(object sender, System.EventArgs e)
{
string Connstr="Data Source=localhost;
uid=sa;
pwd=;
Initial Catalog=Test";
SqlConnection myDSN = new SqlConnection(Connstr);
myDSN.Open();
//int imgid = int.Parse(Request.QueryString["id"]);
string sqlText = "SELECT img_name, img_data, img_contenttype FROM imagelist order by id";//where id="+imgid;
Trace.Write(sqlText);
SqlCommand MyCommand = new SqlCommand (sqlText, myDSN);
SqlDataReader dr =MyCommand.ExecuteReader();
while(dr.Read())
{
Response.ContentType = (dr["img_contenttype"].ToString());
Response.BinaryWrite((byte[])dr["img_data"]);
Response.Write("<br>");
}
myDSN.Close();
}
#region Web Form Designer generated code
override protected void OnInit(EventArgs e)
{
//
// CODEGEN:该调用是 ASP.NET Web 窗体设计器所必需的。
//
InitializeComponent();
base.OnInit(e);
}
/// <summary>
/// 设计器支持所需的方法 - 不要使用代码编辑器修改
/// 此方法的内容。
/// </summary>
#endregion
}
}