首页
学习
活动
专区
圈层
工具
发布
社区首页 >问答首页 >从httppostedfile转换为htmlinputfile

从httppostedfile转换为htmlinputfile
EN

Stack Overflow用户
提问于 2012-01-03 04:51:29
回答 1查看 1.4K关注 0票数 0

如何将httppostedfile转换为htmlinputfile?我正在使用一个旧的混乱的应用程序,到目前为止,我已经能够重构它,使它变得有意义,但这个特殊的混乱太复杂了,不值得付出努力:S。

像往常一样,谢谢你的帮助

相关代码:

集合:

代码语言:javascript
复制
Imports System.IO
Imports System.Web.UI.HtmlControls
Public Class ArchivosCollection
    Inherits CollectionBase

    Default Public Property Item(ByVal index As Integer) As HtmlInputFile
        Get
            Return MyBase.List(index)
        End Get
        Set(ByVal Value As HtmlInputFile)
            MyBase.List(index) = Value
        End Set
    End Property

    Public Function Add(ByVal oArchivo As HtmlInputFile) As Integer
        Return MyBase.List.Add(oArchivo)
    End Function

    Public Function getDataSource() As DataTable
        Dim dt As New DataTable
        Dim oArchivo As HtmlInputFile
        Dim fila As DataRow
        Dim orden As Integer = 0
        dt.Columns.Add("documento", GetType(System.String))
        dt.Columns.Add("tipo", GetType(System.String))
        For Each oArchivo In list
            If Not oArchivo.Disabled Then
                fila = dt.NewRow()
                fila("documento") = Trim(Path.GetFileName(oArchivo.PostedFile.FileName))
                fila("tipo") = Trim(oArchivo.PostedFile.ContentType)
                dt.Rows.Add(fila)
            End If
        Next
        Return dt
    End Function

    Public Function ExisteArchivo(ByVal Nombre As String) As Boolean
        For Each oArchivo As HtmlInputFile In list
            If Not oArchivo.Disabled Then
                If Path.GetFileName(oArchivo.PostedFile.FileName) = Nombre Then
                    Return True
                End If
            End If
        Next
        Return False
    End Function

    Public Function EliminarArchivo(ByVal Nombre As String) As Boolean
        For Each oArchivo As HtmlInputFile In list
            If Not oArchivo.Disabled Then
                If Path.GetFileName(oArchivo.PostedFile.FileName) = Nombre Then
                    oArchivo.Disabled = True
                    Return True
                End If
            End If
        Next
    End Function
End Class

旧代码:

代码语言:javascript
复制
Private Sub btnAgregarDocumento_Click(ByVal sender As System.Object, ByVal e As System.EventArgs) Handles btnAgregarDocumento.Click
    Try
        If Not (Me.fleDocumento.PostedFile Is Nothing) Then
            If Trim(Me.fleDocumento.PostedFile.FileName) = "" Then
                Throw New Exception(rm.GetString("errorDebeEscogerUnArchivo"))
            End If
            If Not Servicios.isValidUploadType(Me.fleDocumento.PostedFile.FileName, ConfigurationManager.AppSettings("Filtros Upload")) Then
                Throw New Exception(rm.GetString("errorExtensionNovalida"))
            End If
            Dim oArchivosOT As ArchivosCollection
            If Session("oArchivosOT") Is Nothing Then
                oArchivosOT = New ArchivosCollection
            Else
                oArchivosOT = Session("oArchivosOT")
            End If
            If oArchivosOT.ExisteArchivo(Path.GetFileName(Me.fleDocumento.PostedFile.FileName)) Then
                Throw New Exception(rm.GetString("errorArchivoYaExiste"))
            End If
            oArchivosOT.Add(Me.fleDocumento)
            Me.dgDocumentos.DataSource = oArchivosOT.getDataSource()
            Me.dgDocumentos.DataBind()
            Session("oArchivosOT") = oArchivosOT
            If Request.QueryString("desde") = "proy" Then
                ClientScript.RegisterStartupScript(Page.GetType, "msg", "<script>window.opener.document.Form1.refGridDocs.click();</script>")
            End If
        Else
            Throw New Exception(rm.GetString("errorDebeEscogerUnArchivo"))
        End If
    Catch exc As Exception
        ClientScript.RegisterStartupScript(Page.GetType, "msg", Servicios.MsgBox(exc.Message))
    End Try
End Sub

新代码(部分):

代码语言:javascript
复制
Dim archivos As HttpFileCollection = Request.Files
            Dim colArchivos As ArchivosCollection = IIf(Session("oArchivosOT") Is Nothing, New ArchivosCollection(), Session("oArchivosOT"))
            Dim i
            For i = 0 To archivos.Count
                colArchivos.Add(DirectCast(archivos(i), HtmlInputFile))
            Next

            Session("oArchivosOT") = colArchivos
EN

回答 1

Stack Overflow用户

回答已采纳

发布于 2012-01-03 05:13:01

HtmlInputFile对象的PostedFile属性是一个HttpPostedFile对象-您可以为每个HtmlInputFile简单地访问它。

票数 3
EN
页面原文内容由Stack Overflow提供。腾讯云小微IT领域专用引擎提供翻译支持
原文链接:

https://stackoverflow.com/questions/8705218

复制
相关文章

相似问题

领券
问题归档专栏文章快讯文章归档关键词归档开发者手册归档开发者手册 Section 归档