Jag listar upp alla filer i en katalog och dessa visas i en datagrid. Jag undrar hur jag ska göra för att filerna ska laddas ner när man klickar på dem. Lite tips, ej komplett.. Löste det på ett annorlunda sätt.Påtvingad nedladdning
<code>
Sub Page_Load(ByVal sender As Object, ByVal e As System.EventArgs) Handles Me.Load
Dim dirInfo As New IO.DirectoryInfo(Server.MapPath("/prog/loin/ljud"))
docList.DataSource = dirInfo.GetFiles("*.doc")
docList.DataBind()
End Sub
<asp:DataGrid runat="server" id="articleList" Font-Name="Verdana"
AutoGenerateColumns="False" AlternatingItemStyle-BackColor="#eeeeee"
HeaderStyle-BackColor="darkorange" HeaderStyle-ForeColor="White"
HeaderStyle-Font-Size="10pt" HeaderStyle-Font-Bold="True">
<Columns>
<asp:HyperLinkColumn DataNavigateUrlField="Name" DataTextField="Name"
HeaderText="Filnamn" />
<asp:BoundColumn DataField="LastWriteTime" HeaderText="Skapad"
ItemStyle-HorizontalAlign="Center" DataFormatString="{0:d}" />
<asp:BoundColumn DataField="Length" HeaderText="Storlek"
ItemStyle-HorizontalAlign="Right"
DataFormatString="{0:#,### bytes}" />
</Columns>
</code>Sv: Påtvingad nedladdning
<code>
Response.AddHeader("content-disposition", "attachment; filename=" & Filename)
Dim b() as Byte = ReadFileIntoByteArray() 'här får du skriva lite kod själv..
Response.BinaryWrite(b)
</code>Sv:Påtvingad nedladdning
Skapade download.aspx:
<code>
<%@ Page language="vb" runat="server" explicit="true" strict="true" %>
<script language="vb" runat="server">
Sub Page_Load(ByVal sender As Object, ByVal e As System.EventArgs) Handles Me.Load
Dim strRequest As String = Request.QueryString("file")
If strRequest <> "" Then
Dim path As String = Server.MapPath(strRequest)
Dim file As System.IO.FileInfo = New System.IO.FileInfo(path)
If file.Exists Then
Response.Clear()
Response.AddHeader("Content-Disposition", "attachment; filename=" & file.Name)
Response.AddHeader("Content-Length", file.Length.ToString())
Response.ContentType = "application/octet-stream"
Response.WriteFile(file.FullName)
Response.End()
Else
Response.Write("This file does not exist.")
End If
Else
Response.Write("Please provide a file to download.")
End If
End Sub
</script>
</code>
Och här är datagriden:
<code>
<asp:DataGrid runat="server" id="articleList" Font-Name="Verdana"
AutoGenerateColumns="False" AlternatingItemStyle-BackColor="#eeeeee"
HeaderStyle-BackColor="darkorange" HeaderStyle-ForeColor="White"
HeaderStyle-Font-Size="10pt" HeaderStyle-Font-Bold="True" Font-Names="Verdana" Font-Size="8pt">
<Columns>
<asp:HyperLinkColumn DataNavigateUrlField="Name" DataTextField="Name" DataNavigateUrlFormatString="download.aspx?file={0}"
HeaderText="Filnamn" />
<asp:BoundColumn DataField="LastWriteTime" HeaderText="Skapad" DataFormatString="{0:d}">
<ItemStyle HorizontalAlign="Center"></ItemStyle>
</asp:BoundColumn>
<asp:BoundColumn DataField="Length" HeaderText="Storlek"
DataFormatString="{0:#,### bytes}">
<ItemStyle HorizontalAlign="Right"></ItemStyle>
</asp:BoundColumn>
</Columns>
<HeaderStyle ForeColor="White" Font-Size="10pt" Font-Bold="True" BackColor="DarkOrange"></HeaderStyle>
<AlternatingItemStyle BackColor="#EEEEEE"></AlternatingItemStyle>
</asp:DataGrid>
</code>