You must Sign In to post a response.
  • Category: ASP.NET

    How To Display a Attachment file from database in a blank page using asp.net

    Hi , Developers
    I am stored all attachments in my database.
    Now i want to display that attachments in a blank page, By clicking a Button or a Link in asp.net.
    I have tried but i have no luck.
    So friends please help me to am finish this task.

    Thanks with
    Paul.S
  • #765043
    Hi

    Can you share for how to stored Attachment file in your database.

    For ex: binary data type? or path ?

    How is stored them then we can retrieve and show

    Name : Dotnet Developer-2015
    Email Id :kumaraspcode2009@gmail.com

    'Not by might nor by power, but by my Spirit,' says the LORD Almighty.

  • #765052
    i want to stored Binary Data Type format Sir.

  • #765056
    Hi Paul,

    If you want to store the files using ASP.net we have 2 options one is store the file in one physical folder in the project solution and store the physical path in database or else store the image as a binary format and store it in database.

    As per your requirement you want to store it in binary format, refer below link this might be give detailed explanation about how to store the image in binary format and how to retrieve the data from database and show it in front end etc....

    brainbrushups.com/2013/06/how-to-save-image-in-binary-format-in.html

    Hope this will helpful to you...

    --------------------------------------------------------------------------------
    Give respect to your work, Instead of trying to impress your boss.

    N@veen
    Blog : http://naveens-dotnet.blogspot.in/

  • #765063

    You already stored the data in the database in the form of binary. So there is no issue in the storing part.

    Now display the list of attachments
    1. You can get the list of attachments.
    2. Display all the document names.
    3. Make the document names as link
    4. In the link you can specify src as ashx handler with document id
    5. Create ashx handler.
    6. Pass the document id to the created ashx handler.
    7. Retrieve document from the DB and write the data into the binary form. In this your have to specify the content type. Content type is important for this part.

    Following is th sample code

    //DB Connection part
    using (con)
    {
    con.Open();
    byte[] file = (byte[])DS.Tables[0].Rows[0][0];
    string Filename = DS.Tables[0].Rows[0][1].ToString();
    try
    {
    context.Response.Clear();
    context.Response.AddHeader
    ("Content-Disposition","inline;attachment; filename=\"" + Filename + "\"");
    context.Response.AddHeader("Content-Length", file.Length.ToString());
    context.Response.ContentType = "application/octet-stream";
    context.Response.BinaryWrite(file);
    context.Response.Flush();
    }
    catch (Exception ex)
    {
    context.Response.ContentType = "text/plain";
    context.Response.Write(ex.Message);
    }

    }
    }


    By Nathan
    Direction is important than speed

  • #765066
    Mr.Nathan i have been tried your code , But it throw the error like follow

    Cannot find table 0.

    using (con)
    Line 68: {
    Line 69: byte[] file = (byte[])DS.Tables[0].Rows[0][0];
    Line 70: string Filename = DS.Tables[0].Rows[0][1].ToString();
    Line 71: try


    Could you please tell me what i can do for solve this type of error.

    Thanks with
    Paul.S

  • #765067
    ah!

    DS.Tables[0].Rows[0][0] is the sample code . you are select query is different right?

    byte[] file is the bytes which you are storing in the database. You can write you own ADO.net code. and assign the values

    //DB Connection part
    using (con)
    {
    con.Open();
    byte[] file = // Your Attachement binaray values selected from the DB
    string Filename = // Attachement name
    try
    {
    context.Response.Clear();
    context.Response.AddHeader
    ("Content-Disposition","inline;attachment; filename=\"" + Filename + "\"");
    context.Response.AddHeader("Content-Length", file.Length.ToString());
    context.Response.ContentType = "application/octet-stream";
    context.Response.BinaryWrite(file);
    context.Response.Flush();
    }
    catch (Exception ex)
    {
    context.Response.ContentType = "text/plain";
    context.Response.Write(ex.Message);
    }


    By Nathan
    Direction is important than speed

  • #765076

    Have you already all files in database as binary data ? if yes then you can retrieve them from database and stored them in individual files and can open them
    read below snippet
    - first read data from database in datatable or in dataset and then call below snippet

    private void download (DataTable dt)
    {
    Byte[] bytes = (Byte[])dt.Rows[0]["Data"];
    Response.Buffer = true;
    Response.Charset = "";
    Response.Cache.SetCacheability(HttpCacheability.NoCache);
    Response.ContentType = dt.Rows[0]["ContentType"].ToString();
    Response.AddHeader("content-disposition", "attachment;filename="
    + dt.Rows[0]["Name"].ToString());
    Response.BinaryWrite(bytes);
    Response.Flush();
    Response.End();
    }


    Thanks
    Koolprasd2003
    Editor, DotNetSpider MVM
    Microsoft MVP 2014 [ASP.NET/IIS]

  • #765078
    Thanks Mr.Prasad


Sign In to post your comments