0

下午所有,asp.net文件上傳(如何創建超鏈接載的文件,在我的GridView中顯示)

我有我已經完成了我的視覺正在開發一個網站一個簡單的文件上傳教程工作室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)節。

任何幫助,使上載文件中可用的這個靜態列表可以保存到一個鏈接到這些單個文檔的列表中,非常受歡迎。

提前許多感謝, 貝蒂

回答

1

Tweat你的代碼是這樣的:

生成標籤:

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>" 

希望它有幫助。

+0

貝蒂非常歡迎你! – CoderRoller

相關問題