首页 > 编程 > .NET > 正文

一个加强的文件上传(VB.Net)

2024-07-10 13:01:07
字体:
来源:转载
供稿:网友
这个文件上传应该可以满足一般的需要了,其中的添加到数据库记录,您可以按需要省略掉。

代码: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>
<tr>
<td bgcolor="#cccccc" colspan="2">
<div class="expandable" onclick="ontoggletoclevel1()" level2id="compileroutputdiv" style="font-weight: bold; cursor: hand; color: navy; text-decoration: underline">显示高级选项:</div>
<div id="compileroutputdiv" style="display: none;">
<br><table width=100% bgcolor="#ffffcc">
<tr>
<td>请输入上传到服务器的目录名称:<input id = "dir" type = "text" runat = "server">【可以自定义上传目录,形式如:upload_1,将新建一个upload_1目录,请确定您有上传到此目录的权限。当前程序目录:<font color=red><b><%response.write (trim((server.mappath(""))))%>/</b></font>】</td>
</td></tr></table></div>
</td>
</tr>
<tr>
<td bgcolor="#cccccc" colspan="2">
 <input type = "submit" value = "开始上传" onserverclick="uploadfile_clicked" runat ="server">
<span id="messagenotxls" enableviewstate="false" style="font: arial 11pt;" runat="server"/></td>
</tr>
</tbody>
</table>
</form>
</asp:panel>

<asp:panel id = "answermsg" visible = " false " runat = "server">
<meta http-equiv=refresh content='5;url=admin_xls_list.aspx'>
 文件上传完成 <br>
 成功上传 <asp:label id = "filename" runat = "server" /> <br>
 文件大小 <asp:label id = "filelength" runat = "server" /> 字节 <br>  文件类型 <asp:label id = "filetype" runat = "server" /> <br>
5秒之后自动返回....
<br>
<span id="message" enableviewstate="false" style="font: arial 11pt;" runat="server"/>
</asp:panel>

<script language="javascript">
function ontoggletoclevel1()
{
var elemsrc = window.event.srcelement;
var elemlevel2 = document.all(elemsrc.level2id);

if (elemlevel2.style.display == 'none')
{
elemlevel2.style.display = '';
if (elemsrc.usesglyph == '1')
elemsrc.innerhtml = '&#054;';
}
else {
elemlevel2.style.display = 'none';
if (elemsrc.usesglyph == '1')
elemsrc.innerhtml = '&#052;';
}
}
</script>

</body>
</html>





收集最实用的网页特效代码!

发表评论 共有条评论
用户名: 密码:
验证码: 匿名发表