2012-01-02 21 views
0

如何將httppostedfile轉換爲htmlinputfile?我正在處理一個應用程序的舊雜亂,到目前爲止已經能夠重構它,所以它有一定的意義,但是這個特定的混亂太糾結了,不值得付出努力:S。從httppostedfile轉換爲htmlinputfile

感謝,像往常一樣,爲幫助

相關代碼:

集合:

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 

舊代碼:

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 

新代碼(部分):

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 
+2

這個問題對我沒有意義 - 'HtmlInputFile'是一個控件。 'HttpPostedFile'允許訪問與它一起上傳的文件。你不能在兩者之間進行轉換。發佈您遇到問題的代碼會更有用,並且可以準確解釋您正在嘗試執行的操作。 – Oded 2012-01-02 20:54:52

+0

我們正試圖在我們的一些頁面中實現多文件上傳。現在文件上傳使用一個自定義的htmlinputfile集合存儲在一個會話變量,所以它可以被破壞和強姦約100左右的頁面,然後最終被存儲在其中的文件的功能訪問,並在其1000代碼行。因爲這個集合有htmlinputfile作爲它的類型,並且我需要在明天早上這樣做,我猜想最好的方法是將其轉換爲htmlinputfile。我會在我的問題中發佈相關代碼。 – 2012-01-02 21:06:56

+0

請同時添加您評論的相關信息(!?)。 – Oded 2012-01-02 21:08:13

回答

3

HtmlInputFile對象的PostedFile屬性是一個HttpPostedFile對象 - 您可以簡單地訪問它的每個HtmlInputFile

+0

看完你的評論後,我想到了很多。 謝謝! =) – 2012-01-02 21:15:52

+0

此屬性是隻讀的。有沒有什麼辦法從httppostedfile創建一個htmlinputfile? – 2012-01-02 21:18:49

+0

@NicolásStraubValdivieso - 不是我所知道的。 – Oded 2012-01-02 21:20:21