上傳文件的步驟:
首先
第二
第三
第一步
首先我們要實現用戶在浏覽器端自由選擇文件
下面是上傳頁面的主要代碼
? <form id=
<span>Title</span><br>
<asp:textbox id=
<asp:requiredfieldvalidator id=
?<br>
?<br>
<span>Docutment to Upload</span><br>
<input id=
<br>
<br>
<asp:button id=
</form>
第二步
我們可以將上傳的文件通過數據流保存到緩存
? int intDocLen = txtFileContents
然後
? byte[] Docbuffer = new byte[intDoclen];
這樣設置以後
Stream objStream;
objStream = txtFileContents
objStream
在以上代碼中
第三步
? 現在我們需要做的就是將緩存數據保存到數據庫
cmdUploadDoc = new SqlCommand(
cmdUploadDoc
cmdUploadDoc
cmdUploadDoc
cmdUploadDoc
cmdUploadDoc
cmdUploadDoc
點擊按鈕處理代碼
private void btnSubmit_Click(object sender
{
? string strDocExt;
//strDocType用於保存上傳文件的類型
? string strDocType;
//用於保存文件大小
int intDocLen;
//Stream用於讀取上傳數據
Stream objStream;
SqlConnection BooksConn;
SqlCommand cmdUploadDoc;
if(IsValid)
{
? if(txtFileContents
{
? //文件類型
? strDocExt = CString
? (txtFileContents
switch(strDocExt)
{
case
strDocType =
break;
case
strDocType =
break;
case
strDocType =
break;
case
strDocType =
break;
case
strDocType =
break;
case
strDocType =
break;
default:
strDocType =
break;
}
//上傳文件具體內容
intDocLen = txtFileContents
byte[] Docbuffer = new byte[intDocLen];
objStream = txtFileContents
//文件保存到緩存
//緩存將保存到數據庫
objStream
BooksConn = new?
?SqlConnection(
cmdUploadDoc = new?
SqlCommand(
cmdUploadDoc
cmdUploadDoc
cmdUploadDoc
cmdUploadDoc
cmdUploadDoc
cmdUploadDoc
cmdUploadDoc
BooksConn
cmdUploadDoc
BooksConn
}
}
}
From:http://tw.wingwit.com/Article/program/ASP/201405/30779.html