Friday, February 18, 2011

Download file from griedview cell link in asp.net

1. Add gridview

<asp:GridView ID="GridView1" runat="server" SkinID="gridviewSkin" DataKeyNames="UploadID,UserId,AllocatedTo,AllocatedforQC"
                    OnRowCommand="GridView1_RowCommand" 
                    OnRowDataBound="GridView1_RowDataBound"
                    OnPageIndexChanging="GridView1_PageIndexChanging">
                    <Columns>
                        <asp:BoundField  HeaderText="Job#" DataField="uploadID" /> 
                        <asp:TemplateField HeaderText="Client Name-Uploaded By/ <br>Uploaded Date/<br>Duration/<br>File Size/<br>" ItemStyle-HorizontalAlign="Left" HeaderStyle-HorizontalAlign="left">
                            <ItemTemplate>
                                <b><%#Eval("ClientName")%></b>-<%#Eval("FullName")%><img src="images/spacer.gif" alt="" width="10"  height="13px" /><br />
                               
                                <%#Eval("CLIENT_UPLOAD_DATE")%><img src="images/spacer.gif" alt="" width="10" height="13px" /><br />
                               
                                <%#Eval("Duration")%> [HH:MM:SS]<img src="images/spacer.gif" alt="" width="10" height="13px" /><br />
                               
                                <%#Eval("fileSize")%> kb<img src="images/spacer.gif" alt="" width="10" height="13px" /><br />
                            </ItemTemplate>
                        </asp:TemplateField>
                         <asp:BoundField  HeaderText="Transcribe By" DataField="ALLOCATEDTONAME" />
                       <asp:TemplateField HeaderText="Transcription File" ItemStyle-HorizontalAlign="Center" HeaderStyle-Width="5%">
                            <ItemTemplate>
                                <asp:LinkButton Text='<img src="Images/downloadaudio.gif" alt="" border="0">' runat="server" ID="lnkDownload" CommandArgument='<%#Eval("CLIENT_UPLOAD_FILENAME") %>' CommandName="lnkDownload" BorderStyle="None" BorderWidth="0"></asp:LinkButton>
                            </ItemTemplate>
                        </asp:TemplateField>
                        <asp:TemplateField HeaderText="Download File(s)<br> for QC" HeaderStyle-Width="2%" ItemStyle-HorizontalAlign="Center">
                        <ItemTemplate>
                            <asp:Image ID="FileImage1" CssClass="handcursor" runat="server" ImageUrl="Images/downloaddoc.gif" onclick='<%#Eval("UPLOADID", "return ShowPopup(\"{0}\")")%>' />
                        </ItemTemplate>
                            <HeaderStyle Width="2%"></HeaderStyle>
                            <ItemStyle Wrap="true" />
                        </asp:TemplateField>
                       <%--<asp:TemplateField HeaderText="Upload" ItemStyle-HorizontalAlign="Center" HeaderStyle-Width="5%">
                            <ItemTemplate>
                            <asp:LinkButton ID="lnkUpload" runat="server" Text="Upload" ></asp:LinkButton>
                            </ItemTemplate>
                        </asp:TemplateField>--%>
                        <asp:TemplateField HeaderText="Accept/Reject QC" ItemStyle-HorizontalAlign="Center" HeaderStyle-Width="5%">
                            <ItemTemplate>
                            <%--<asp:LinkButton ID="lnkAcceptQC" runat="server" Text="Release to QC" CommandArgument='<%#Eval("UploadID") %>' CommandName="AcceptQC"></asp:LinkButton>--%>
                                <asp:LinkButton ID="lnkAccept" runat="server" Text="Release to Client" OnClientClick="return confirmB();"  CommandArgument='<%#Eval("UploadID") %>' CommandName="Accept"></asp:LinkButton>
                                <br /><br />
                                <asp:LinkButton ID="lnkReject" runat="server" Text="Reject" CommandArgument='<%#Eval("UploadID") %>' CommandName="Reject"></asp:LinkButton>
                            </ItemTemplate>
                        </asp:TemplateField>
                       
                        <asp:BoundField HeaderText="Status" HeaderStyle-Width="10%" DataField="STATUS" ItemStyle-HorizontalAlign="Center">
                            <HeaderStyle Width="8%"></HeaderStyle>
                            <ItemStyle Wrap="true" />
                        </asp:BoundField>
                    </Columns>
                </asp:GridView>

2. add code in code behind

protected void GridView1_RowCommand(object sender, GridViewCommandEventArgs e)
{
 if (e.CommandName.Equals("lnkDownload"))
        {
           
            string strFilePath = System.Configuration.ConfigurationManager.AppSettings["uploadDictationFolder"].ToString();
            //+ "/" + e.CommandArgument.ToString()
           DownloadFile(e.CommandArgument.ToString(),strFilePath);
            //Response.Write(strFilePath);
         
        }
}

    public int DownloadFile(string fileName, string Path)
        {
            HttpResponse response = HttpContext.Current.Response;
            FileInfo file = null;
            int messageToReturn = 0;
            string path = Path + "\\" + fileName;
            try
            {
                file = new FileInfo(path);
                if (file.Exists)
                {
                    response.AddHeader("Content-Disposition", "attachment; filename=\"" + file.Name + "\"");
                    response.AddHeader("Content-Length", file.Length.ToString());
                    response.ContentType = GetContentType(file);
                    response.TransmitFile(file.FullName);
                    response.Buffer = true;
                    response.End();
                    messageToReturn = (int)FileHandlerEnum.Success;
                    messageToReturn = (int)FileHandlerEnum.Success;
                }
                else
                {
                    messageToReturn = (int)FileHandlerEnum.FailedFileNotExist;
                }
            }
            catch (IOException ex)
            {
                throw ex;
            }
            catch (Exception ex)
            {
                throw ex;
            }
            finally
            {
                //file = null;
                //response.End();
            }
            return messageToReturn;
        }

1 comment: