CSDN博客

img lonz

upload.aspx

发表于2004/10/14 15:52:00  1706人阅读

代码: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>

最后演示如下:

阅读全文
1 0

相关文章推荐

img
取 消
img