会员
周边
众包
新闻
博问
闪存
赞助商
所有博客
当前博客
我的博客
我的园子
账号设置
简洁模式
...
退出登录
注册
登录
Skying Blue @ cnblogs
Be a programmer here. Atlas, ASP.NET, .NET, Javascript, PHP, and C#
博客园
::
首页
::
新随笔
::
联系
::
订阅
::
管理
公告
彻底解决asp.net多附件上传
上传
现将源码公布
aspx页面代码:
<
%@ Page Language
=
"
vb
"
AutoEventWireup
=
"
false
"
Codebehind
=
"
Upload.aspx.vb
"
Inherits
=
"
Test.Upload
"
%
>
<
!DOCTYPE HTML
PUBLIC
"
-//W3C//DTD HTML 4.0 Transitional//EN
"
>
<
HTML
>
<
HEAD
>
<
title
>
Upload
</
title
>
<
meta name
=
"
GENERATOR
"
content
=
"
Microsoft Visual Studio .NET 7.0
"
>
<
meta name
=
"
CODE_LANGUAGE
"
content
=
"
Visual Basic 7.0
"
>
<
meta name
=
"
vs_defaultClientScript
"
content
=
"
JavaScript
"
>
<
meta name
=
"
vs_targetSchema
"
content
=
"
http://schemas.microsoft.com/intellisense/ie5
"
>
<
script language
=
"
javascript
"
>
Function addFiles()
function
addFiles(oContainer)
{
var sLineHTML
=
"
<div><input type='file' name='files' style='width:228'><input type='button' onclick='javascript:delFileInput(this)' value='删除'></div>
"
;
oContainer.insertAdjacentHTML(
'
beforeEnd',sLineHTML);
}
Function delFileInput()
function
delFileInput(oInputButton)
{
var divToDel
=
oInputButton.parentNode;
divToDel.parentNode.removeChild(divToDel);
}
</
script
>
</
HEAD
>
<
body MS_POSITIONING
=
"
GridLayout
"
>
<
form id
=
"
Form1
"
method
=
"
post
"
runat
=
"
server
"
encType
=
"
multipart/form-data
"
>
<
table align
=
"
center
"
>
<
tr
>
<
td align
=
"
middle
"
><
h1
>
多附件上传 作者:Bt之家 cjlwxy
</
h1
>
</
td
>
</
tr
>
<
tr
>
<
td id
=
"
TD
"
>
<
INPUT
style
=
"
WIDTH: 300px
"
type
=
"
file
"
name
=
"
Files
"
>
<
BUTTON style
=
"
WIDTH: 79px; HEIGHT: 20px
"
onclick
=
"
javascript:addFiles(TD);
"
type
=
"
button
"
>
继续添加
</
BUTTON
>
</
td
>
</
tr
>
<
tr
>
<
td
>
<
asp:Label ID
=
"
lblError
"
Runat
=
"
server
"
></
asp:Label
>
</
td
>
</
tr
>
<
tr
>
<
td align
=
"
middle
"
>
<
asp:Button ID
=
"
btnUpLoad
"
Runat
=
"
server
"
Text
=
"
上 传
"
EnableViewState
=
"
False
"
CausesValidation
=
"
true
"
></
asp:Button
>
</
td
>
</
tr
>
</
table
>
</
form
>
</
body
>
</
HTML
>
aspx.vb代码:
Imports
System.IO
Public
Class Upload
Class
Upload
Inherits
System.Web.UI.Page
Protected
WithEvents
lblError
As
System.Web.UI.WebControls.Label
Protected
WithEvents
btnUpLoad
As
System.Web.UI.WebControls.Button
Web 窗体设计器生成的代码
#Region
" Web 窗体设计器生成的代码 "
'
该调用是 Web 窗体设计器所必需的。
<
System.Diagnostics.DebuggerStepThrough()
>
Private
Sub InitializeComponent()
Sub
InitializeComponent()
End Sub
Private
Sub Page_Init()
Sub
Page_Init(
ByVal
sender
As
System.Object,
ByVal
e
As
System.EventArgs)
Handles
MyBase
.Init
'
CODEGEN: 此方法调用是 Web 窗体设计器所必需的
'
不要使用代码编辑器修改它。
InitializeComponent()
End Sub
#End Region
Private
Sub Page_Load()
Sub
Page_Load(
ByVal
sender
As
System.Object,
ByVal
e
As
System.EventArgs)
Handles
MyBase
.Load
'
在此处放置初始化页的用户代码
End Sub
Private
Sub btnUpLoad_Click()
Sub
btnUpLoad_Click(
ByVal
sender
As
System.Object,
ByVal
e
As
System.EventArgs)
Handles
btnUpLoad.Click
Dim
sFilesName
As
String
Dim
oPostedFile
As
HttpPostedFile
Dim
n
As
Integer
Dim
i
As
Integer
n
=
Request.Files.Count()
For
i
=
0
To
n
-
1
oPostedFile
=
Request.Files.Item(i)
sFilesName
=
UpLoadMoreFile(oPostedFile,
"
TestDic
"
)
Select
Case
sFilesName
Case
""
Case
"
InValid
"
lblError.Text
=
lblError.Text
+
"
<font color='red'>文件</font>:
"
&
oPostedFile.FileName
&
"
<font color='red'> 不合法!</font><br>
"
Case
"
Failure
"
lblError.Text
=
lblError.Text
+
"
<font color='red'>文件</font>:
"
&
oPostedFile.FileName
&
"
<font color='red'> 上传失败!</font><br>
"
Case
Else
lblError.Text
=
lblError.Text
+
"
<font color='red'>文件</font>:
"
&
oPostedFile.FileName
&
"
<font color='red'> 上传成功!</font><br>
"
End
Select
Next
i
End Sub
'
+++++++++++++++++++++++++++++++++++++++++++++++++++++++
'
名称:UpLoadMoreFile
'
说明:上传文件
'
参数:poFile : 上传文件输入域名,如:txtPicture
'
psPath : 上传虚拟引用路径,如:Pictures
'
返回:InValid:上传文件无效
'
Failure:上传文件失败,捕获异常
'
空:不上传文件
'
其它:上传文件成功
'
'
cjlwxy 2005-10-12
'
+++++++++++++++++++++++++++++++++++++++++++++++++++++++
Function UpLoadMoreFile()
Function
UpLoadMoreFile(
ByVal
poFile
As
System.Web.HttpPostedFile,
ByVal
psPath
As
String
)
As
String
Dim
sPath
As
String
Dim
sFileName
As
String
Dim
**
t
As
String
'
扩展名
'
首先判断文件输入域的合法性
If
Trim
(poFile.FileName)
=
""
Then
Return
""
'
不上传文件
Else
If
poFile.ContentLength
=
0
Then
'
文件不合法或者文件不正确,无法上传
Return
"
InValid
"
Else
'
根据用户选择的文件名生成新的服务器文件名称
sFileName
=
poFile.FileName()
Dim
nBackSlash
As
Integer
nBackSlash
=
sFileName.LastIndexOf(
"
.
"
)
If
nBackSlash
<>
-
1
Then
'
取文件名后缀
**
t
=
sFileName.Substring(nBackSlash)
'
以所经过的毫秒数为文件名
sFileName
=
Now.Ticks.ToString
&
**
t
End
If
'
获取唯一文件名
sPath
=
System.Web.HttpContext.Current.Server.MapPath(psPath)
sFileName
=
GetUniqueFileName(sPath, sFileName)
'
上传文件
Try
poFile.SaveAs(sPath
&
"
\
"
&
sFileName)
Return
sFileName
Catch
oException
As
Exception
Throw
oException
Return
"
Failure
"
End
Try
End
If
End
If
End Function
'
+++++++++++++++++++++++++++++++++++++++++++++++++++++++
'
名称:GetUniqueFileName
'
说明:获取上传文件的唯一名
'
参数:poFile : 上传文件名
'
psPath : 上传实际路径
'
返回:唯一文件名(String)
'
'
cjlwxy 2005-10-12
'
+++++++++++++++++++++++++++++++++++++++++++++++++++++++
Public
Shared
Function GetUniqueFileName()
Function
GetUniqueFileName(
ByVal
psPath
As
String
,
ByVal
psFileName
As
String
)
As
String
Dim
sFile
As
String
'
文件名
Dim
**
t
As
String
'
扩展名
Dim
n
As
Integer
=
1
Dim
nLastDot
As
Integer
sFile
=
psFileName
nLastDot
=
sFile.LastIndexOf(
"
.
"
)
If
nLastDot
=
-
1
Then
**
t
=
""
Else
**
t
=
sFile.Substring(nLastDot)
sFile
=
sFile.Substring(
0
, nLastDot)
End
If
Do
While
File.Exists(psPath
&
"
\
"
&
sFile
&
**
t)
sFile
=
sFile
&
n
n
=
n
+
1
Loop
Return
sFile
&
**
t
End Function
End Class
posted on
2006-06-15 17:45
flyluo
阅读(
1388
) 评论(
0
)
编辑
收藏
举报
会员力量,点亮园子希望
刷新页面
返回顶部