VB.NET上传附件代码

 '附件添加 按钮 点击事件  吴翰哲 2013年7月23日 16:53:19
        Protected Sub BtnAddFile_Click(ByVal sender As Object, ByVal e As EventArgs) Handles BtnAddFile.Click
            Try
                Dim sysConfigDB As New SysConfigDB
                Dim dr As DataRow = sysConfigDB.getItemByName("MaxFileSize")
                Dim maxFileSize As Integer
                If dr("ItemValue").ToString() = "" Or IsNumeric(dr("ItemValue").ToString()) = False Then
                    Dim returnVal As Integer = sysConfigDB.update("MaxFileSize", "4")
                    If returnVal < 1 Then
                        Exit Sub
                    End If
                End If
                dr = sysConfigDB.getItemByName("MaxFileSize")
                maxFileSize = Integer.Parse(dr("ItemValue").ToString())
                Dim strFlag As String

                If Me.UP_FILE.PostedFile.ContentLength > 0 Then
                    If Me.UP_FILE.PostedFile.ContentLength > (maxFileSize * 1000000) Then
                        'errMsg.Text = "附件大于4兆,无法上传"
                        lblError.Text = "附件大于" & (maxFileSize) & "兆,无法上传"
                        Exit Sub
                    End If
                    strFileName = Me.UP_FILE.PostedFile.FileName
                    strFileName = strFileName.Substring(strFileName.LastIndexOf("\") + 1)

                    '先上传文件
                    objFile1.FileLength = CStr(Me.UP_FILE.PostedFile.ContentLength)

                    strFlag = Upload3(2, Me.UP_FILE)
                    If strFlag = "" Then
                        lblError.Text = "附件保存失败!"
                        Exit Sub
                    Else
                        'If FileName = "" Then
                        'FileName = objFile1.FileName
                        'Else
                        '    FileName = FileName + ";" + objFile1.FileName
                        'End If

                        If OraFileName = "" Then
                            OraFileName = strFileName
                        Else
                            OraFileName = OraFileName + ";" + strFileName
                        End If
                        '单一上传文件,不支持一次批量上传
                        BtnAddFile.Visible = False
                        UP_FILE.Visible = False
                        '绑定drp
                        Dim ditem As New ListItem(strFileName, objFile1.FileName)
                        DropFiles.Items.Add(ditem)
                        ViewState("upSucess") = "upSucess"
                        lblError.Text = ""
                    End If
                    End If

                '下来判断是否是“更新文档”,来决定是否要添加到SQL 中去

            Catch ex As IO.IOException
                Throw New Exception(ex.InnerException.Message + ex.Message)
            Catch ex As Exception
                Throw New Exception(ex.Message)
            End Try
        End Sub


 '得到保存附件的路径及文件名
 Public Overridable Function rootPath3() As String
           
            If IsNothing(ViewState.Item("rootPath")) Then
                rootPath3 = Server.MapPath(Context.Request.ApplicationPath) & "\File\PaperOfTeacherFile\"
                Try
                    If Not System.IO.Directory.Exists(rootPath3) Then
                        System.IO.Directory.CreateDirectory(rootPath3)
                    End If

                Catch ex As IO.IOException

                    Throw New AppException("无法上传文件!可能是由于用户没有创建文件夹的权限。", ex)
                    Exit Function
                End Try

                ViewState.Add("rootPath3", rootPath3)
            Else
                rootPath3 = ViewState.Item("rootPath3")
            End If
        End Function



 Public Overridable Function Upload3(ByVal Type As Integer, ByVal Up_File As HtmlInputFile) As String
            '创建和更新都只用这个方法,这样可以不用判断是那种情况

            If Up_File Is Nothing Then
                Return 0 '没有选择上传文件
            End If

            Dim Upfile As HttpPostedFile = Up_File.PostedFile
            '********************保存文件到指定的文件夹下***********************
            If Not IsNothing(Upfile) Then
                If String.IsNullOrEmpty(Upfile.FileName) Then
                    lblError.Text = "请选择附件后再点‘下一步’!"
                    Return String.Empty
                End If

                Dim nam As String = System.IO.Path.GetFileName(Upfile.FileName.Trim)
                If nam.LastIndexOf(".") < 0 Then
                    lblError.Text = "请点击‘浏览’来选择附件!"
                    Return String.Empty
                End If

                '文件名全名:为了防止多人同时上传文件,当前时间末尾加上一个随机数。
                Dim newFileName As String = Date.Now.Ticks.ToString & CStr(Int((6 * Rnd()) + 1)) & System.IO.Path.GetExtension(nam) '取得文件名扩展名

                '文件保存到 /file/Bord/时间/ 目录下,文件名为上传时间+随机数+文件类型

                Upfile.SaveAs(Me.rootPath3 & newFileName)
                ViewState("FileName") = newFileName
                FileName = newFileName
                Return newFileName
            End If

            Return ""
        End Function



'附件删除按钮 点击事件 吴翰哲 2013年7月25日 10:51:43
        Protected Sub btnDelFile_Click(ByVal sender As Object, ByVal e As EventArgs) Handles btnDelFile.Click
            If DropFiles.Items.Count > 0 Then

                Dim delFileName As String
                delFileName = DropFiles.SelectedItem.Value
                'Me.DropFiles.Items.Remove(DropFiles.SelectedItem)
                Dim delFileobj As New FileUploadOutline
                'delFileobj.DeleteOldFile(delFileName)
                'delFileobj.DelFileinSQL(LabelMessageID.Text, delFileName)


                Try
                    Dim delStr As String = delFileobj.DeleteOldFile3(delFileName)

                    If delStr <> "ok" Then
                        lblError.Text = "删除文件时有错误发生,删除失败!"
                        Exit Sub
                    End If
                    Dim rint As Integer = delFileobj.DelFileinSQL3(DropFiles.SelectedItem.Text, DropFiles.SelectedValue)
                    If rint <> 1 Then
                        lblError.Text = "请确定已上传文件,删除失败!"
                        lblSucess.Text = ""
                        Exit Sub
                    End If
                Catch ex As IO.IOException
                    Throw New AppException("删除文件失败可能是由于用户没有创建文件夹的权限。", ex)
                    Exit Sub
                End Try
                ViewState("attnum") = ViewState("attnum") - 1
                Me.DropFiles.Items.Remove(DropFiles.SelectedItem)
                lblSucess.Text = "删除附件成功"
                lblError.Text = ""
                BindRptFile()
                UP_FILE.Visible = False
                BtnAddFile.Visible = False
                'Btnupdate_Click(sender, e)
            End If
        End Sub



 Public Overridable Function DeleteOldFile3(ByVal FileName As String) As String
            If Not FileName Is Nothing Then
                If System.IO.File.Exists(Server.MapPath(Context.Request.ApplicationPath) & "\File\PaperOfTeacherFile\" & FileName) Then
                    System.IO.File.Delete(Server.MapPath(Context.Request.ApplicationPath) & "\File\PaperOfTeacherFile\" & FileName)
                    Return "ok"
                End If
            End If
            Return ""
        End Function

 

转载于:https://www.cnblogs.com/WuHZ/p/3248363.html

  • 0
    点赞
  • 0
    收藏
    觉得还不错? 一键收藏
  • 0
    评论

“相关推荐”对你有帮助么?

  • 非常没帮助
  • 没帮助
  • 一般
  • 有帮助
  • 非常有帮助
提交
评论
添加红包

请填写红包祝福语或标题

红包个数最小为10个

红包金额最低5元

当前余额3.43前往充值 >
需支付:10.00
成就一亿技术人!
领取后你会自动成为博主和红包主的粉丝 规则
hope_wisdom
发出的红包
实付
使用余额支付
点击重新获取
扫码支付
钱包余额 0

抵扣说明:

1.余额是钱包充值的虚拟货币,按照1:1的比例进行支付金额的抵扣。
2.余额无法直接购买下载,可以购买VIP、付费专栏及课程。

余额充值