【问题标题】:asp.net file upload (how to create a hyperlink to uploaded documents, displayed in my gridview)asp.net文件上传(如何创建上传文件的超链接,显示在我的gridview中)
【发布时间】:2012-05-15 14:41:28
【问题描述】:

下午,

我有一个简单的文件上传教程,我已经为我在 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) 部分添加一些额外的代码。

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

提前非常感谢, 贝蒂

【问题讨论】:

    标签: asp.net visual-studio visual-studio-2010 asp.net-controls


    【解决方案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    
    

    如果您希望在您的文件名上生成“&lt;a&gt;”标签,您可以通过这种方式更改每个文件名:

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

    希望对你有帮助。

    【讨论】:

    猜你喜欢
    • 2016-04-08
    • 1970-01-01
    • 1970-01-01
    • 1970-01-01
    • 2013-08-13
    • 2015-12-12
    • 1970-01-01
    • 1970-01-01
    • 1970-01-01
    相关资源
    最近更新 更多