插入|二进制|上传|上传图片|数据|数据库|显示|二进制|上传|上传图片
前台shangchuan.aspx代码:
<% Page language="c#" Codebehind="shangchuan.aspx.cs" AutoEventWireup="false" Inherits="hualong.allknow.shangchuan" %>
<!DOCTYPE HTML PUBLIC "-//W3C//DTD HTML 4.0 Transitional//EN" >
<HTML>
<HEAD>
<title>shangchuan</title>
<meta name="GENERATOR" Content="Microsoft Visual Studio .NET 7.1">
<meta name="CODE_LANGUAGE" Content="C#">
<meta name="vs_defaultClientScript" content="JavaScript">
<meta name="vs_targetSchema" content="http://schemas.microsoft.com/intellisense/ie5">
</HEAD>
<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" RUNAT="server" ACCEPT="text/*" NAME="up_file" id="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" TEXT="Upload Image" ID="Button1" />
</TD>
</TR>
</TABLE>
</FORM>
</BODY>
</HTML>
下面是后台shangchuan.aspx.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 hualong.allknow
{
/// <summary>
/// shangchuan 的摘要说明。
/// </summary>
public class shangchuan : System.Web.UI.Page
{
protected System.Web.UI.WebControls.TextBox txtDescription;
protected System.Web.UI.WebControls.Label txtMessage;
protected System.Web.UI.WebControls.Button Button1;
protected System.Web.UI.HtmlControls.HtmlInputFile up_file;
protected Int32 fileLength = 0;
private void Page_Load(object sender, System.EventArgs e)
{
// 在此处放置用户代码以初始化页面
}
#region Web 窗体设计器生成的代码
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.Load += new System.EventHandler(this.Page_Load);
}
#endregion
private void Button1_Click(object sender, System.EventArgs e)
{
HttpPostedFile upFile = up_file.PostedFile;//HttpPostedFile对象,用来读取上传图片的属性
fileLength = upFile.ContentLength;//记录文件的长度
try
{
if(fileLength==0)//当文件长度为0的时候
{
txtMessage.Text = "请选择要上传的文件!";
}
else
{
byte[] fileByte = new byte[fileLength];//用图片的长度来初始化一个字节数组存储临时的图片文件
Stream fileStream = upFile.InputStream;//建立文件流对象
&nbs