如何在ASP.NET里用HtmlInputFile控件来上载文件。
(華版)
以前要在ASP里上载文件真的要写一大堆程序。。。比如说把文件分段转换成Base64然后才上载到服务器,而在服务器那边由要从Base64转会原本的byte值才可以分段写如文件中。但是在ASP.NET里就完全只需用一行代码加一个HtmlInputFile控件就能做完这些复杂的程序。
而要注意的就是紧记在包含HtmlInputFile控件的Form里多添加一个enctype的属性,enctype="multipart/form-data";然后再用HtmlInputFile控件里的PostedFile.SaveAs方法来上载文件就可以了。
<form id="Form1" method="post" runat="server" enctype="multipart/form-data">
</form>
</form>
'声明变量
Dim filename As String
Dim data() As String
Try
'检查所选文件大小
If PostFileName.PostedFile.ContentLength = 0 Then
'显示上载结果
Label3.Text = "上载失败!"
Else
'显示所选文件资料
Label2.Text = "文件大小:" & PostFileName.PostedFile.ContentLength & " bytes<br>"
Label2.Text = Label2.Text & "文件类型:" & PostFileName.PostedFile.ContentType & "<br>"
Label2.Text = Label2.Text & "文件名称:" & PostFileName.PostedFile.FileName & "<br>"
'读取文件名称
data = Split(PostFileName.PostedFile.FileName, "\")
'上载文件到服务器
PostFileName.PostedFile.SaveAs(Server.MapPath(".") & "\" & data(UBound(data)))
'显示上载结果
With Label3
.Text = "上载成功!"
.ForeColor = Color.DarkGreen
End With
End If
Catch ex As Exception
'显示错误讯息
With Label3
.Text = "Error: " & ex.Message
.ForeColor = Color.Red
End With
End Try
Dim filename As String
Dim data() As String
Try
'检查所选文件大小
If PostFileName.PostedFile.ContentLength = 0 Then
'显示上载结果
Label3.Text = "上载失败!"
Else
'显示所选文件资料
Label2.Text = "文件大小:" & PostFileName.PostedFile.ContentLength & " bytes<br>"
Label2.Text = Label2.Text & "文件类型:" & PostFileName.PostedFile.ContentType & "<br>"
Label2.Text = Label2.Text & "文件名称:" & PostFileName.PostedFile.FileName & "<br>"
'读取文件名称
data = Split(PostFileName.PostedFile.FileName, "\")
'上载文件到服务器
PostFileName.PostedFile.SaveAs(Server.MapPath(".") & "\" & data(UBound(data)))
'显示上载结果
With Label3
.Text = "上载成功!"
.ForeColor = Color.DarkGreen
End With
End If
Catch ex As Exception
'显示错误讯息
With Label3
.Text = "Error: " & ex.Message
.ForeColor = Color.Red
End With
End Try
整体来说,最为麻烦的也不过是要从PostedFile.FileName里读取真整的文件名称(不包括括file path);不过这也大可用以下的简短代码来完成。
'读取文件名称
data = Split(PostFileName.PostedFile.FileName, "\")
'文件名称
Debug.Print data(UBound(data))
data = Split(PostFileName.PostedFile.FileName, "\")
'文件名称
Debug.Print data(UBound(data))
如果要上载多个文件(为可更有效的重用代码),那可以用Request.File来读取每一个所选的文件名称;就如以下的代码。
'声明变量
Dim lIndex As Integer
Dim lCount As Integer
Dim hpf As HttpPostedFile
Dim filename As String
Dim data() As String
Try
'读取所有要上载文件的数目
lCount = Request.Files.Count
'读取每一个上载文件的资料
For lIndex = 0 To lCount - 1
'读取第N个上载文件的资料
hpf = Request.Files(lIndex)
'检查所选文件大小
If hpf.ContentLength = 0 Then
'显示所选文件资料
Label2.Text = Label2.Text & "<br>" & "文件大小:" & hpf.ContentLength & " bytes<br>"
Label2.Text = Label2.Text & "文件类型:" & hpf.ContentType & "<br>"
Label2.Text = Label2.Text & "文件名称:" & hpf.FileName & "<br>"
'读取文件名称
data = Split(hpf.FileName, "\")
'上载文件到服务器
PostFileName.PostedFile.SaveAs(Server.MapPath(".") & "\" & data(UBound(data)))
'显示上载结果
With Label3
.Text = "上载成功!"
.ForeColor = Color.DarkGreen
End With
End If
'释放资源
hpf = Nothing
Next
Catch ex As Exception
'显示错误讯息
With Label3
.Text = "Error: " & ex.Message
.ForeColor = Color.Red
End With
End Try
Dim lIndex As Integer
Dim lCount As Integer
Dim hpf As HttpPostedFile
Dim filename As String
Dim data() As String
Try
'读取所有要上载文件的数目
lCount = Request.Files.Count
'读取每一个上载文件的资料
For lIndex = 0 To lCount - 1
'读取第N个上载文件的资料
hpf = Request.Files(lIndex)
'检查所选文件大小
If hpf.ContentLength = 0 Then
'显示所选文件资料
Label2.Text = Label2.Text & "<br>" & "文件大小:" & hpf.ContentLength & " bytes<br>"
Label2.Text = Label2.Text & "文件类型:" & hpf.ContentType & "<br>"
Label2.Text = Label2.Text & "文件名称:" & hpf.FileName & "<br>"
'读取文件名称
data = Split(hpf.FileName, "\")
'上载文件到服务器
PostFileName.PostedFile.SaveAs(Server.MapPath(".") & "\" & data(UBound(data)))
'显示上载结果
With Label3
.Text = "上载成功!"
.ForeColor = Color.DarkGreen
End With
End If
'释放资源
hpf = Nothing
Next
Catch ex As Exception
'显示错误讯息
With Label3
.Text = "Error: " & ex.Message
.ForeColor = Color.Red
End With
End Try
All the posts in this blog are provided "AS IS" with no warranties, and confer no rights. Except where otherwise noted, content on this site is licensed under a Creative Commons Attribution 2.5 China Mainland License.