当前位置:首页>>ASP.NET教程>>Asp.NET综合技巧>>一个加强的文件上传(VB.Net)
一个加强的文件上传(VB.Net)
2009/11/8 13:19:18
这个文件上传应该可以满足一般的需要了,其中的添加到数据库记录,您可以按需要省略掉。

代码:upload.ASPx

<%@ Page Language="VB"%>

<% @ Import Namespace=" System.IO " %>
<% @ Import Namespace=" System.DATA " %>
<%@ import Namespace="System.Data.SqlClient" %>
<SCRIPT LANGUAGE = "VB" RUNAT = "SERVER">
Dim lstrFileFolder as string
Dim lstrFileFolder_1 as string
  Dim lstrFileNamePath as string
  Dim lstrFileNamePath_1 as string
 Sub UploadFile_Clicked ( Sender as Object, e as EventArgs )
  Dim lstrFileName as string

  'Dim lstrFileFolder as string
Dim CMDfilename as string '表单cmdfilename的值
CMDfilename = request("CMDfilename")
'''
'''
  ' 如果上传目录为空,就使用" upload "作为缺省上传目录
  ' 获得上传到服务器的目录名称
  if dir.value <> "" then
   lstrFileFolder_1 = dir.value & "/"
lstrFileFolder = trim((Server.MapPath(""))) & "\" & dir.value & "\"
  else
lstrFileFolder_1 = "upload/"
   lstrFileFolder = trim((Server.MapPath(""))) &"\upload\"
  end if

  ' 获得文件名称
  lstrFileName = loFile.PostedFile.FileName

   ' 注: loFile.PostedFile.FileName 返回的是
   '通过文件对话框选择的文件名,这之中包含了文件的目录信息

'只能上传.xls文件
if instr(Path.GetFileName ( lstrFileName ),".xls")=0 and instr(Path.GetFileName ( lstrFileName ),".Xls")=0 and instr(Path.GetFileName ( lstrFileName ),".XLS")=0 then
MessageNotXLS.InnerHTML = "只能上传.xls文件"
MessageNotXLS.Style("color") = "red"
exit sub
end if

if CMDfilename = "random" then
dim lstrFileName111 as string = Path.GetFileName ( lstrFileName ) '获得原名
lstrFileName=now()
lstrFileName=replace(lstrFileName,"-","")
lstrFileName=replace(lstrFileName,":","")
lstrFileName=replace(lstrFileName," ","")
'lstrFileName=lstrFileName+"."
lstrFileName=lstrFileName & right(Path.GetFileName ( lstrFileName111 ),len(Path.GetFileName ( lstrFileName111 ))-(instr(Path.GetFileName ( lstrFileName111 ),"."))+1)
else
  lstrFileName = Path.GetFileName ( lstrFileName )
end if
   ' 去掉目录信息,返回文件名称

  ' 判断上传目录是否存在,不存在就建立
  If ( not Directory.Exists ( lstrFileFolder ) ) Then
   Directory.CreateDirectory ( lstrFileFolder )
  End If

   '上传文件到服务器
  lstrFileNamePath = lstrFileFolder & lstrFileName
lstrFileNamePath_1 = lstrFileFolder_1 & lstrFileName
   ' 得到上传目录及文件名称
  loFile.PostedFile.SaveAs ( lstrFileNamePath )

   ' 获得并显示上传文件的属性
  FileName.Text = lstrFileName
   ' 获得文件名称
  FileType.Text = loFile.PostedFile.ContentType
   ' 获得文件类型
  FileLength.Text = cStr ( loFile.PostedFile.ContentLength )
   ' 获得文件长度
  FileUploadForm.visible = false
  AnswerMsg.visible = true
   ' 显示上传文件属性
AddToDB()
  End sub

'添加记录信息到数据库,可以不要这步。
sub AddToDB()
Dim MyConnection As SQLConnection = New SQLConnection (ConfigurationSettings.AppSettings("ConnStr"))
Dim InsertCmd As String = "insert into chaoqi (title,filepath) values (@title,@filepath)"
Dim MyCommand As SqlCommand = new SqlCommand(InsertCmd, MyConnection)

MyCommand.Parameters.Add(New SqlParameter("@title", SqlDbType.NVarChar, 50))
MyCommand.Parameters("@title").Value = title.value
MyCommand.Parameters.Add(New SqlParameter("@filepath", SqlDbType.NVarChar, 50))
MyCommand.Parameters("@filepath").Value = lstrFileNamePath_1
MyCommand.Connection.Open()
Try
MyCommand.ExecuteNonQuery()
Message.InnerHTML = "<b>已添加记录</b><br>" & InsertCmd.ToString()
Catch Exp As SQLException
If Exp.Number = 2627
Message.InnerHTML = "错误:已存在具有相同主键的记录"
Else
Message.InnerHTML = "错误:未能添加记录,请确保正确填写了字段"
End If
Message.Style("color") = "red"

End Try

MyCommand.Connection.Close()
end sub

</SCRIPT>
<HTML>
<head>
<title>上传文件</title>
<link href=../../"style/main.CSS" type="text/CSS" rel="stylesheet" />
</head>
<BODY>
<ASP:panel id = "FileUploadForm" visible = " true " runat = "server">
<form method = "post" enctype = "multipart/form-data" runat = "server">
<p>
</p>
<table style="BORDER-RIGHT: #999999 1px; BORDER-TOP: #999999 1px; BORDER-LEFT: #999999 1px; WIDTH: 100%; BORDER-BOTTOM: #999999 1px; BORDER-COLLAPSE: collapse; BACKGROUND-COLOR: white" bordercolor="#999999" cellspacing="0" cellpadding="3" rules="cols" border="1">
<tbody>
<tr>
<td bgcolor="#000084" colspan="2">
<font color="#ffffff"><b>超期记录文件上传(Excel文件)</b></font></td>
</tr>
<tr>
<td width="20%">
请输入该文件要显示的名称:</td>
<td>
<input id = "title" type = "text" runat = "server"></td>
</tr>
<tr>
<td width="20%" bgcolor="#cccccc">
请选择上传到服务器的文件:</td>
<td bgcolor="#cccccc"><input id = "loFile" type = "file" runat = "server"></td>
</tr>
<tr>
<td width="20%" bgcolor="#eeeeee">
匹配类型:</td>
<td bgcolor="#eeeeee">
随机生成文件名(无重复):
<input type="radio" checked="checked" value="random" name="CMDfilename" id="CMDfilename" />
使用原名(若重复则覆盖):
<input type="radio" value="NotRandom" name="CMDfilename" id="CMDfilename" />
</td>
</tr>