Protected Void Gridview1_rowcommand ( Object Sender, gridviewcommandeventargs E)
{
// The button in the template column needs to be set to commandargument = "<% # gridview1.rows. Count %>". Otherwise, an error occurs.
If (E. commandname = " Download " )
{
Filedownload ( @" F: \ music \ 6-thirsty " );
}
}
Private Void Filedownload ( String Fullfilename)
{
Fileinfo downloadfile = New Fileinfo (fullfilename );
Response. Clear ();
Response. clearcontent ();
Response. clearheaders ();
Response. Buffer = False ;
Response. contenttype = " Application/octet-stream " ;
Response. appendheader ( " Content-Disposition " , " Attachment; filename = " + Httputility. urlencode (downloadfile. fullname, system. Text. encoding. utf8 ));
Response. appendheader ( " Content-Length " , Downloadfile. length. tostring ());
Response. addheader ( " Content-transfer-Encoding " , " Binary " );
Response. contentencoding = System. Text. encoding. getencoding ( " UTF-8 " );
Response. writefile (downloadfile. fullname );
Response. Flush ();
Response. End ();
}
Note:
If it is in the gridview under updatepanel, you need to add the followingCode:
<Triggers>
<ASP: postbacktrigger controlid="Gridview1" />
</Triggers>
Otherwise, an error is reported.