连接文件名

时间:2010-07-12 16:15:34

标签: asp.net vb.net

我在子窗口中有以下代码正在工作,但我想要做的不是使用response.write我想使用标签控件或显示所有文件名,如下所示:

music.pdf,inventory.doc

我的最终目标是将字符串中的值(例如:“music.pdf,inventory.pdf”)传递给父窗口。

我该怎么做?

Protected Sub btnUpload_Click(ByVal sender As Object, ByVal e As System.EventArgs) Handles btnUpload.Click
    Try
        '' Get the HttpFileCollection
        Dim hfc As HttpFileCollection = Request.Files
        For i As Integer = 0 To hfc.Count - 1
            Dim hpf As HttpPostedFile = hfc(i)
            If hpf.ContentLength > 0 Then
                hpf.SaveAs(Server.MapPath("/ServerName/DirectoryName") & "\"" & System.IO.Path.GetFileName(hpf.FileName))
                Response.Write("<b>File: </b>" & hpf.FileName & " <b>Size:</b> " & hpf.ContentLength & " <b>Type:</b> " & hpf.ContentType & " Uploaded Successfully! <br/>")
            Else
                Response.Write("Please select a file to upload.")
            End If
        Next i

    Catch ex As Exception

    End Try

End Sub

7 个答案:

答案 0 :(得分:1)

考虑使用<asp:BulletedList>,它是无序列表<ul>的ASP.NET服务器控件。

<强> ASPX:

<asp:BulletedList 
   id="uploadedFiles" runat="server"></asp:BulletedList>

而不是Response.Write()每个文件,只需:

  Dim displayText as String = string.Format("File: {0}  Size: {1}  Type: {2} Uploaded Successfully", _
          hpf.FileName ,hpf.ContentLength ,hpf.ContentType)

  uploadedFiles.Items.Add(New ListItem(displayText, hpf.FileName ))

答案 1 :(得分:1)

这不是我的方式,但是你要求你的代码使用Label。

<强> ASPX:

<asp:Label id="lblUploadMsg" runat="server" visible="false"></asp:Label>

<强>代码:

Protected Sub btnUpload_Click(ByVal sender As Object, ByVal e As System.EventArgs) Handles btnUpload.Click
    Try
        '' Get the HttpFileCollection
        Dim hfc As HttpFileCollection = Request.Files

        lblUploadMsg.Text = String.empty
        For i As Integer = 0 To hfc.Count - 1
            Dim hpf As HttpPostedFile = hfc(i)
            If hpf.ContentLength > 0 Then
                hpf.SaveAs(Server.MapPath("/ServerName/DirectoryName") & "\"" & System.IO.Path.GetFileName(hpf.FileName))
                lblUploadMsg.Visible = True;                   
                lblUploadMsg.Text +="<b>File: </b>" & hpf.FileName & " <b>Size:</b> " & hpf.ContentLength & " <b>Type:</b> " & hpf.ContentType & " Uploaded Successfully! <br/>"


            Else
                lblUploadMsg.Text="Please select a file to upload."
            End If
        Next i

        ''HTML Encode lblUploadingMsg.Text after

    Catch ex As Exception

    End Try

End Sub

答案 2 :(得分:1)

在表单/控件上添加一个标签控件,然后不使用Response.Write,为什么不使用字符串生成器并将所有输出附加到循环中的输出。处理完所有文件后,设置字符串生成器ToString方法的Label控件的Text属性。

希望这有帮助

答案 3 :(得分:0)

您可以使用其中一个String.Join()重载。

答案 4 :(得分:0)

使用字符串构建器来连接字符串。 .NET中的字符串是不可变的,因此Join和任何其他运算符都是内存密集型的

评论(不是关于问题,但我在你的代码中注意到它)使用Path.Combine和任何其他Path函数。很容易忘记他们存在并为我节省了数百万的头痛。

答案 5 :(得分:0)

Response.Write移到i以外的循环之外。

i循环内部,使用List<String>跟踪。然后,在循环之后,使用Response.Write,使用逗号在String.Join上写List.ToArray

答案 6 :(得分:0)

使用Label控件不会尊重您的<b>标签 - 您在这里寻找的是使用Literal控件。尝试这样的事情:

在你的标记中:

<asp:literal runat="server" id="UploadedFileInfoLiteral" />

在您的代码隐藏中:

Protected Sub btnUpload_Click(ByVal sender As Object, ByVal e As System.EventArgs) Handles btnUpload.Click
Try
    '' Get the HttpFileCollection
    Dim hfc As HttpFileCollection = Request.Files
    Dim FileInfoStringBuilder As System.Text.StringBuilder = New System.Text.StringBuilder
    Dim hpf As HttpPostedFile

    For i As Integer = 0 To hfc.Count - 1
        hpf = hfc(i)
        If hpf.ContentLength > 0 Then
            hpf.SaveAs(Server.MapPath("/ServerName/DirectoryName") & "\"" & System.IO.Path.GetFileName(hpf.FileName))
            FileInfoStringBuilder.Append("<strong>File: </strong>" & hpf.FileName & " <strong>Size:</strong> " & hpf.ContentLength & " <strong>Type:</strong> " & hpf.ContentType & " Uploaded Successfully! <br/>")
        Else
            FileInfoStringBuilder.Append("Please select a file to upload.")
        End If
    Next

    UploadedFileInfoLiteral.Text = FileInfoStringBuilder.ToString()

Catch ex As Exception

End Try

End Sub