0

下午所有,

我有一个简单的文件上传教程,我已经为我在 Visual Studio 2010 中开发的网站完成了该教程。

我只想通过使文件成为gridview中该文档的链接来增强显示上传文件的gridview。这将使用户能够上传文件并通过按网格视图中的链接来查看文件来查看文件。

到目前为止,这是我的代码...

<form id="form1" runat="server">
<div> 
<table style="width: 90%"> 
   <tr> 
      <td style="width: 100px"> Single File Upload:<br />
      <asp:FileUpload ID="FileUpload1" runat="server" /><br />
      <asp:Button ID="buttonUpload" runat="server" Text="Upload" /><br />
<br />
       <asp:GridView ID="UploadedFiles" DataSource="<%# GetUploadList() %>" runat="server" CellPadding="4" ForeColor="#333333" GridLines="None"> 
        <FooterStyle BackColor="#507CD1" Font-Bold="True" ForeColor="White" />
        <RowStyle BackColor="#EFF3FB" />
        <EditRowStyle BackColor="#2461BF" />
        <SelectedRowStyle BackColor="#D1DDF1" Font-Bold="True" ForeColor="#333333" />
        <PagerStyle BackColor="#2461BF" ForeColor="White" HorizontalAlign="Center" />
        <HeaderStyle BackColor="#507CD1" Font-Bold="True" ForeColor="White" />
        <AlternatingRowStyle BackColor="White" />
      </asp:GridView>
    </td>
  </tr>
</table>
</div>
</form> 

这是.VB页面

Partial Class test
Inherits System.Web.UI.Page

Protected Sub Page_Load(ByVal sender As Object, ByVal e As System.EventArgs) Handles Me.Load
    If Not IsPostBack Then
        UploadedFiles.DataBind()
    End If
End Sub

    Protected Function GetUploadList() As String()
    Dim folder As String = Server.MapPath("~/Uploads")
    Dim files() As String = Directory.GetFiles(folder)
    Dim fileNames(files.Length - 1) As String
    Array.Sort(files)

    For i As Integer = 0 To files.Length - 1
        fileNames(i) = Path.GetFileName(files(i))
    Next

    Return fileNames
End Function

 Protected Sub UploadThisFile(ByVal upload As FileUpload)
    If upload.HasFile Then
        Dim theFileName As String = Path.Combine(Server.MapPath("~/Uploads"), upload.FileName)
        If File.Exists(theFileName) Then
            File.Delete(theFileName)
        End If
        upload.SaveAs(theFileName)
    End If
End Sub

Protected Sub buttonUpload_Click(ByVal sender As Object, ByVal e As System.EventArgs) Handles buttonUpload.Click
    UploadThisFile(FileUpload1)
    UploadedFiles.DataBind()
End Sub

End Class

我不太确定如何在该Protected Sub UploadThisFile(ByVal upload As FileUpload)部分中添加一些额外的代码。

将上传文件中可用的此静态项目列表添加到包含指向这些单个文档的链接的列表的任何帮助都非常有用。

非常感谢,贝蒂

4

1 回答 1

1

以这种方式调整您的代码:

生成标签:

Protected Function GetUploadList() As String()

        Dim folder As String = Server.MapPath("~/Uploads")
        Dim files() As String = Directory.GetFiles(folder)
        Dim fileNames(files.Length - 1) As String
        Dim lnk As String = String.Empty
        Array.Sort(files)

        For i As Integer = 0 To files.Length - 1
            lnk = "  <a href=""Uploads/" & files(i).ToString() & """ target=""_blank"">File</a>"
            fileNames(i) = Path.GetFileName(files(i)) & lnk
            lnk = ""
        Next

        Return fileNames

End Function

访问每个 gridview 单元格以解码标记:

Protected Sub UploadedFiles_RowDataBound(ByVal sender As Object, ByVal e As System.Web.UI.WebControls.GridViewRowEventArgs) Handles UploadedFiles.RowDataBound

        If e.Row.RowType = DataControlRowType.DataRow Then

            Dim tcls As TableCellCollection = e.Row.Cells

            For Each tc As TableCell In tcls
                tc.Text = Server.HtmlDecode(tc.Text)
            Next

        End If

End Sub    

如果您希望<a>在您的文件名上生成“”标签,您可以通过以下方式更改每个文件名:

fileNames(i) = "<a href=""Uploads/" & Path.GetFileName(files(i).ToString()) & """ target=""_blank"">" & Path.GetFileName(files(i)) & "</a>"

希望能帮助到你。

于 2012-05-15T15:24:44.593 回答