Reputation: 1931
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.
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.
Upvotes: 0
Views: 1346
Reputation: 1931
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();
}
Upvotes: 0
Reputation: 6514
The return type of GetData
should be the same as the type of your DocData
property, i.e. byte[]
.
Upvotes: 1