0

I am saving my word/excel/pdf/Img files in SQL using binary and i am able to successfully save the files as binary.

var fileSize = FileUpload1.PostedFile.ContentLength;
var documentBinary = new byte[fileSize];
var u = new UDocument
                {
                    DocName = fileName.ToString(CultureInfo.InvariantCulture),
                    Type = documentType.ToString(CultureInfo.InvariantCulture),
                    DocData = documentBinary
                };
                u.DocID = ++count;
                sd.UDocuments.InsertOnSubmit(u);
                sd.SubmitChanges();

Now, i am trying to open the binary as their document type, and i am opening this from the gridview where i am displaying all the stored files.

enter image description here

Now, in Gridview SelectedIndexChange Event, i am able to get the document ID, document name, for me to open the file,but i am unclear about how to grab the binary data from the documentID, which is the PK, and how to write the file out. Here is the method i am trying to make it to work:

protected void GridView1_SelectedIndexChanged(object sender, EventArgs e)
    {
        int docid = Convert.ToInt16(GridView1.SelectedRow.Cells[1].Text);
        string name = GridView1.SelectedRow.Cells[2].Text;
        Response.Clear();
        Response.ContentType = "application/octet-stream";
        Response.AddHeader("Content-Disposition", "attachment;filename=" + name);
        Response.BinaryWrite(GetData(docid)); // I am unclear on how to use this method, with the 
        Response.Flush();
    }

where, in my GetData(int id) method, i am trying to use LINQ to get the correct binary data through the unique docID like this:

var data = from a in sd.UDocuments
            where a.DocID == id
            select a.DocData

but i am not sure what return type to use in this method.

Any help or pointers will be great.

0

2 Answers 2

1

The return type of GetData should be the same as the type of your DocData property, i.e. byte[].

Sign up to request clarification or add additional context in comments.

1 Comment

i spoke too soon, but +1 since you made me to revisit the method i had it earlier..private byte[] GetData(int id) { byte[] data = (from a in sd.UDocuments where a.DocID == id select a.DocData).First().ToArray(); return data; }
0
 private byte[] GetData(int id)
    {
        byte[] data = (from a in sd.UDocuments
            where a.DocID == id
            select a.DocData).First().ToArray();
       return data;
    }

protected void GridView1_SelectedIndexChanged(object sender, EventArgs e)
    {
        int docid = Convert.ToInt16(GridView1.SelectedRow.Cells[1].Text);
        string name = GridView1.SelectedRow.Cells[2].Text;
        string contentType = GridView1.SelectedRow.Cells[3].Text;

        Response.Clear();
        Response.ContentType = contentType; //"application/octet-stream";
        Response.AddHeader("Content-Disposition", "attachment;filename=" + name);
        Response.BinaryWrite(GetData(docid));
        Response.End();
    }

Comments

Your Answer

By clicking “Post Your Answer”, you agree to our terms of service and acknowledge you have read our privacy policy.

Start asking to get answers

Find the answer to your question by asking.

Ask question

Explore related questions

See similar questions with these tags.