Afternoon All,
I have a simple file upload tutorial that i have completed for a site that i am developing in visual studio 2010.
I just want to enhance the gridview that shows the uploaded file by making the file a link to that document in the gridview. This will enable users to then upload the file and view the file by pressing a link in the gridview to view the file.
Here is my code so far...
<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>
And here is the .VB page
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
Im not too sure on how to add a little extra code into the Protected Sub UploadThisFile(ByVal upload As FileUpload)
section.
Any help to make this static list of items that are available in the Uploads file to a list that holds a link to these individual documents is much appriechiated.
Many thanks in advance, Betty
Tweat your code this way:
Generate the tags:
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
Access each gridview cell to decode the markup:
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
If you want the "<a>
" tag to get generated on your file name you could change each filename this way:
fileNames(i) = "<a href=""Uploads/" & Path.GetFileName(files(i).ToString()) & """ target=""_blank"">" & Path.GetFileName(files(i)) & "</a>"
Hope it helps.