CREATE TABLE TupDoc
(
DocID int NOT NULL IDENTITY Primary Key ,
DocTitle varchar (200) ,
Doc image,
DocType varchar (50) ,
Entrydate datetime Default GetDate()
)
----------------------------------------------------------
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.Data.SqlClient;
using System.IO;
namespace longmengOA
{
/// <summary>
/// upfile 的摘要说明。
/// </summary>
public class upfile : System.Web.UI.Page
{
protected System.Web.UI.HtmlControls.HtmlInputFile ftpfiles;
protected System.Web.UI.WebControls.TextBox txtfilename;
protected System.Web.UI.WebControls.Button btnupfile;
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.btnupfile.Click += new System.EventHandler(this.btnupfile_Click);
this.Load += new System.EventHandler(this.Page_Load);
}
#endregion
private void btnupfile_Click(object sender, System.EventArgs e)
{
//if(ftpfiles.PostedFile!=null)
//{
// try
// {
// ftpfiles.PostedFile.SaveAs("c:\\"+TextBoxFileName.Text);
// LabelStat.Text="上传文件成功!";
// }
// catch(Exception exc)
// {
// LabelStat.Text="上传过程中出错!"+exc.ToString();
// }
//}
string strDocExt;
//strDocType用于保存上传文件的类型
string strDocType;
string filetitle=txtfilename.Text;
//用于保存文件大小
int intDocLen;
//Stream用于读取上传数据
Stream objStream;
SqlConnection objConnection;
SqlCommand cmdUploadDoc;
if(ftpfiles.PostedFile != null)
{
//文件类型
// strDocExt = ftpfiles.PostedFile.FileName.GetType.ToLower();
string fileName = Path.GetFileName(ftpfiles.PostedFile.FileName);
strDocExt = System.IO.Path.GetExtension(fileName);
switch(strDocExt)
{
case ".doc":
strDocType = "doc";
break;
case ".ppt":
strDocType = "ppt";
break;
case ".htm":
strDocType = "htm";
break;
case ".html":
strDocType = "htm";
break;
case ".jpg":
strDocType = "jpg";
break;
case ".gif":
strDocType = "gif";
break;
default:
strDocType = "txt";
break;
}
//上传文件具体内容
intDocLen = ftpfiles.PostedFile.ContentLength;
byte[] Docbuffer = new byte[intDocLen];
objStream = ftpfiles.PostedFile.InputStream;
//文件保存到缓存
//缓存将保存到数据库
objStream.Read(Docbuffer ,0,intDocLen);
string connstr="server=(local);Initial Catalog=longmengoa;User ID=sa;Password=lm;";
// string sqlstr="insert into TupDoc(DocTitle,Doc,DocType)values('"+filetitle+"',"+Docbuffer+",'"+strDocType+"')";
string sqlstr="insert into TupDoc(DocTitle,Doc,DocType)values(@title,@Doc,@DocType)";
objConnection = new SqlConnection(connstr);
cmdUploadDoc = new SqlCommand(sqlstr,objConnection);
cmdUploadDoc.Parameters.Add("@title",SqlDbType.VarChar,50);
cmdUploadDoc.Parameters.Add("@Doc",SqlDbType.Image);
cmdUploadDoc.Parameters.Add("@DocType",SqlDbType.VarChar,4);
cmdUploadDoc.Parameters["@title"].Value = filetitle;
cmdUploadDoc.Parameters["@Doc"].Value = Docbuffer ;
cmdUploadDoc.Parameters["@DocType"].Value = strDocType;
objConnection.Open();
cmdUploadDoc.ExecuteNonQuery();
objConnection.Close();
Response.Write("<script language=javascript>alert('上传成功');</script>");
}
}
}
}
