Pages

Wednesday, August 22, 2012

Code to upload or save document and display them in Gridview with a Hyperlink to download

<asp:FileUpload ID="flUpload" runat="server" />&nbsp;(Word/PDF/Excel formats only.)
<asp:Button ID="btnSave" runat="server" Text="Save" OnClick="btnSave_Click"/>

<asp:GridView ID="grdResults" DataKeyNames="doc_id" OnRowCommand="grdResults_RowCommand">
<Columns>
<asp:ButtonField ButtonType="Link" DataTextField="name" CommandName="Download" HeaderText="Document Name" />
</Columns>
</asp:GridView>


protected void btnSave_Click(object sender, EventArgs e)
{
if (flUpload.HasFile)
    {
    if (flUpload.PostedFile.ContentLength < 4194304)//Less than 4MB
    {
        string filePath = flUpload.PostedFile.FileName;
        string filename = Path.GetFileName(filePath);
        string ext = Path.GetExtension(filename);
        string contenttype = String.Empty;
        //Set the contenttype based on File Extension
        switch (ext)
        {
        case ".doc":
            contenttype = "application/vnd.ms-word";
            break;
        case ".docx":
            contenttype = "application/vnd.ms-word";
            break;
        case ".xls":
            contenttype = "application/vnd.ms-excel";
            break;
        case ".xlsx":
            contenttype = "application/vnd.ms-excel";
            break;
        case ".pdf":
            contenttype = "application/pdf";
            break;
        }
        if (contenttype != String.Empty)
        {
        Stream fs = flUpload.PostedFile.InputStream;
        BinaryReader br = new BinaryReader(fs);
        byte[] myByte = br.ReadBytes((Int32)fs.Length);
        //Write your code to save in DB
        }
        else
        {
        lblError.ForeColor = System.Drawing.Color.Red;
        lblError.Text = "File format not recognised." + " Upload Word/PDF/Excel formats only.";
        return;
        }
    }
    else
    {
        lblError.ForeColor = System.Drawing.Color.Red;
        lblError.Text = "File size can not be greater than 4MB.";
        return;
    }
    }
}
protected void grdResults_RowCommand(object sender, GridViewCommandEventArgs e)
{
    if (e.CommandName == "Download")
    {
        string fileName = string.Empty;
        int index = Convert.ToInt32(e.CommandArgument);
        GridViewRow row = grdResults.Rows[index];
        int documentID = Convert.ToInt32(grdResults.DataKeys[index].Value);
        DataView dv = Util.GetDataViewFaqDocs(documentID);//Get data from DB
        if (dv.ToTable().Rows.Count > 0)
        {
        byte[] fileData = (byte[])dv.ToTable().Rows[0]["content"];
        Response.ClearContent();
        Response.AddHeader("Content-Disposition", "attachment; filename=" + dv.ToTable().Rows[0]["name"].ToString());
        BinaryWriter bw = new BinaryWriter(Response.OutputStream);
        bw.Write(fileData);
        bw.Close();
        Response.ContentType = dv.ToTable().Rows[0]["content_type"].ToString();
        Response.End();
        }               
    }
}

No comments:

Post a Comment