十年网站开发经验 + 多家企业客户 + 靠谱的建站团队
量身定制 + 运营维护+专业推广+无忧售后,网站问题一站解决
利用VB.NET向服务器上传文件
创新互联建站主要从事成都做网站、网站设计、外贸营销网站建设、网页设计、企业做网站、公司建网站等业务。立足成都服务崇义,十多年网站建设经验,价格优惠、服务专业,欢迎来电咨询建站服务:18980820575
文件上传技术是一个很实用的技术,有着很广泛的应用,在ASP.NET自身的前一个版本ASP里实现这个功能,就必须使用第三方的组件或者自己开发组件了,现在,用ASP.NET实现起来就简单得多了,不需要使用任何组件就可以实现上传的功能了。
为了方便理解,可以把文件上传分成两种类型:单一文件上传和多文件上传。
单一文件上传
首先单一文件上传的方法,单一文件上传就比较简单了,
下面是实现单一文件上传的完整代码:
%@ Import Namespace="System" %
%@ Import Namespace="System.Web.HttpPostedFile" %
%@ Import Namespace="System.Web.UI.HtmlControls.HtmlInputFile" %
script language="VB" runat="server"
Sub UpLoad(Src As Object, E As EventArgs)
If UploadFile.PostedFile.ContentLength=0 then
ShowUpLoadFile.innerText="上传失败或文件不存在!"
Else
''''获取文件名
dim Temp() as String=Split(UploadFile.PostedFile.FileName,"\")
dim FileName as String=Temp(Temp.Length-1)
''''保存文件
UploadFile.PostedFile.SaveAs(Server.MapPath(".") "\Files\" FileName)
''''显示上传结果
ShowUpLoadFile.InnerHtml="文件上传成功!br上传文件名:" FileName
End If
End Sub
/script
html
body
form runat="server" enctype="multipart/form-data"
input type="file" id="UploadFile" runat="server" size="50"br
asp:button runat="server" Text="立即上传" onClick="Upload" /
/form
hrbr
span id="ShowUpLoadFile" runat="server"/span
/body
/html
把上面的代码保存成.aspx文件,然后在该文件所在目录下创建一个存放文件的新目录Files,运行,先感受一下效果,然后再继续看下面的讲解
使用ASP.NET上传文件,需要用到.NET框架的两个类:HttpPostedFile和HtmlInputFile,这两个类所在的命名空间分别是System.Web.HttpPostedFile和System.Web.UI.HtmlControls.HtmlInputFile,所以我们要在文件开头先导入这两个命名空间,
其中的PostedFile表示上传到服务器的文件,它包含几个常用的属性:
ContentLength:文件大小;
FileName :上传文件的详细路径及文件名;
ContentType :上传文件的文件类型。
字符分割函数Split是用来取得文件名的,因为通过PostedFile.FileName获得的是详细的路径及文件名。
多文件上传
所谓的多文件上传就是同时上传多个文件,这个跟单一文件上传大多是相同的,不同的是多文件上传是把所有文件作为一个文件集合一起上传到服务器的,我们需要的是把这个文件集合分解成一个个单一的文件,剩下的处理方法就跟单一文件上传一样了。
首先要知道要最多同时上传多少个文件,然后就在form之间放多少个如下的HtmlInput控件:
input type="file" runat="server" size="50"
注意:这里的HtmlInput控件控件是不需要设置ID的
那如何在上传到服务器的文件集合中取出一个个的文件?看下面的代码:
dim i as integer
For i=0 to Request.Files.Count-1
‘使用Request.Files()来逐个获取上传的文件
dim myFile as HttpPostedFile=Request.Files(i)
''''这里的myFile就相当于上例中的PostedFile,可以用myFile.FileName获得文件名,etc
''''这里的处理代码就跟单一文件上传的一样了
Next
现在掌握了ASP.NET文件上传技术了
dim a as double
dim b as double
if (string.IsNullOrEmpty(txt1.Text) or string.IsNullOrEmpty(txt2.Text)) begin
messagebox.show("请输入数值");
return;
end
if double.TryParse(txt.Text, out a ) begin
else begin
Messagebox.show('文本框1包含非数字')
return;
end
if double.TryParse(txt2.Text, out b) begin
else
Messagebox.show('文本框2包含非数字')
return;
end
Label1.Text = 0.6*a + 0.4*b;
Imports System
Imports System.Drawing
Imports System.ComponentModel
Imports System.Windows.Forms
Imports System.Data.OleDb
Imports System.Data
Public Class Form1
Inherits Form
Private components As System.ComponentModel.Container = Nothing
Private WithEvents lastrec As Button
Private WithEvents nextrec As Button
Private WithEvents previousrec As Button
Private WithEvents firstrec As Button
Private t_books As TextBox
Private t_nl As TextBox
Private t_xb As TextBox
Private t_xm As TextBox
Private t_id As TextBox
Private l_books As Label
Private l_nl As Label
Private l_xb As Label
Private l_xm As Label
Private l_id As Label
Private label1 As Label
Private myDataSet As DataSet
Private WithEvents button1 As Button
Private WithEvents button2 As Button
Private WithEvents button3 As Button
Private WithEvents button4 As Button
Private myBind As BindingManagerBase
Public Sub New ( )
MyBase.New ( )
GetConnected ( )
InitializeComponent ( )
End Sub
'清除在程序中使用过的资源
Protected Overloads Overrides Sub Dispose (ByVal disposing As Boolean)
If disposing Then
If Not (components Is Nothing) Then
components.Dispose ( )
End If
End If
MyBase.Dispose ( disposing )
End Sub
Public Sub GetConnected ( )
'创建一个数据连接
Dim strCon As String = " Provider = Microsoft.Jet.OLEDB.4.0 ;
Data Source = db.mdb "
Dim myConn As OleDbConnection = New OleDbConnection ( )
myConn.ConnectionString = strCon
Dim strCom As String = " SELECT * FROM person "
'创建一个 DataSet
myDataSet = New DataSet ( )
myConn.Open ( )
'通过OleDbDataAdapter对象得到一个数据集
Dim myCommand As OleDbDataAdapter = New OleDbDataAdapter
( strCom , myConn )
'把Dataset绑定books数据表
myCommand.Fill ( myDataSet , "person" )
'关闭此数据连接
myConn.Close ( )
End Sub
'插入数据记录操作代码
Private Sub button2_Click (ByVal sender As Object , _
ByVal e As System.EventArgs) Handles button2.Click
'判断所有字段是否添完,添完则执行,反之弹出提示
If ( t_id.Text "" And t_xm.Text ""
And t_xb.Text "" And t_nl.Text ""
And t_books.Text "" ) Then
Dim myConn1 As String = " Provider = Microsoft.Jet.OLEDB.4.0 ;
Data Source = db.mdb"
Dim myConn As OleDbConnection = New OleDbConnection ( myConn1 )
myConn.Open ( )
Dim strInsert As String = " INSERT INTO person ( id , xm , xb , nl
, zip ) VALUES ( " _
t_id.Text + " , '" _
t_xm.Text + "' , '" _
t_xb.Text + "' , " _
t_nl.Text + " , " _
t_books.Text + ")"
Dim inst As OleDbCommand = New OleDbCommand ( strInsert , myConn )
inst.ExecuteNonQuery ( )
myConn.Close ( )
myDataSet.Tables ( "person" ).Rows ( myBind.Position ).BeginEdit ( )
myDataSet.Tables ( "person" ).Rows ( myBind.Position ).EndEdit ( )
myDataSet.Tables ( "person" ).AcceptChanges ( )
Else
MessageBox.Show ( "必须填满所有字段值!" , "错误!" )
End If
End Sub