ASP.NET page with a C# code-behind is not working when trying to download a file
Hi this is the current version......please help.
<%@ Page Language="C#" MasterPageFile="~/Site.master" AutoEventWireup="true" CodeFile="FormDownload.aspx.cs" Inherits="Default2" Title="Form Download" %>
<asp:Content ID="Content1" ContentPlaceHolderID="head" Runat="Server">
</asp:Content>
<asp:Content ID="Content2" ContentPlaceHolderID="LoginContent" Runat="Server">
</asp:Content>
<asp:Content ID="Content3" ContentPlaceHolderID="MainContent" Runat="Server">
<asp:GridView ID="GridView1" runat="server" AutoGenerateColumns="False" CellPadding="2"
ForeColor="#333333" GridLines="None" AllowPaging="True"
onselectedindexchanged="GridView1_SelectedIndexChanged" CellSpacing="10"
HorizontalAlign="Center">
<Columns>
<asp:TemplateField>
<ItemTemplate>
<asp:LinkButton ID="lnkdownload" runat="server" Text="Download" CommandName="Download"
CommandArgument='<%#Eval("FullName") +";" + Eval("Name") %>'></asp:LinkButton>
</ItemTemplate>
</asp:TemplateField>
<asp:BoundField DataField="Name" HeaderText="File Name" />
<asp:BoundField DataField="Length" HeaderText="Size (Bytes)" />
</Columns>
<FooterStyle BackColor="#1C5E55" Font-Bold="True" ForeColor="White" />
<RowStyle BackColor="#E3EAEB" />
<PagerStyle BackColor="#666666" ForeColor="White" HorizontalAlign="Center" />
<SelectedRowStyle BackColor="#C5BBAF" Font-Bold="True" ForeColor="#333333" />
<HeaderStyle BackColor="#1C5E55" Font-Bold="True" ForeColor="White" />
<EditRowStyle BackColor="#7C6F57" />
<AlternatingRowStyle BackColor="White" />
</asp:GridView>
</asp:Content>
using System;
using System.Collections;
using System.Configuration;
using System.Data;
using System.Linq;
using System.Web;
using System.Web.Security;
using System.Web.UI;
using System.Web.UI.HtmlControls;
using System.Web.UI.WebControls;
using System.Web.UI.WebControls.WebParts;
using System.Xml.Linq;
using System.IO;
public partial class Default2 : System.Web.UI.Page
{
private void BindGrid()
{
DirectoryInfo dir = new DirectoryInfo(&quo开发者_运维知识库t;D:/Pilabs Projects/GlobeDse/globedse website/globedse.com/Uploads");
FileInfo[] files = dir.GetFiles();
GridView1.DataSource = files;
GridView1.DataBind();
}
protected void Page_Load(object sender, EventArgs e)
{
if (!Page.IsPostBack)
{
BindGrid();
}
}
private void Downloadfile(String fileName, String FullFilePath)
{
Response.AddHeader("Content-Disposition", "attachment; filename=" + fileName);
Response.TransmitFile(FullFilePath);
Response.End();
}
protected void GridView1_RowCommand(object sender, System.Web.UI.WebControls.GridViewCommandEventArgs e)
{
if (e.CommandName == "Download")
{
String[] fileInfo = e.CommandArgument.ToString().Split(';');
String FileName = fileInfo[1].ToString();
String FullPath = fileInfo[0].ToString();
Downloadfile(FileName, FullPath);
}
}
protected void GridView1_SelectedIndexChanged(object sender, EventArgs e)
{
GridView1.PageIndex = e.NewPageIndex
BindGrid();
}
}
First, check if your method Downloadfile
is getting hit. You also did
Response.Write(FullPath);
which kinda ruins your output. I'd rather design it that you redirect to another page which outputs the download.
You're missing a check for postbacks. Your Page_Load should look something like this:
protected void Page_Load(object sender, EventArgs e)
{
if (!Page.IsPostBack)
{
BindGrid();
}
}
When you re-databind the grid, the events are lost.
精彩评论