how to retrive file ,using file info on click event of a
buton and disply it on a web form
Answer Posted / kapil
If your document store in database in binary format then
code for retrive the document is
string sqlQuery="write the select statement"
datareaderObject =cmd.executereader(sqlQuery,connectionname);
if(datareaderObject.read())
{
Response.Buffer = false;
Response.ClearHeaders();
Response.ContentType = "application/octet-stream";
Response.AddHeader("Content-Disposition",
"attachment; filename=" +
datareaderObject["docname"].ToString());
//Code for streaming the object while writing
const int ChunkSize = 1024;
byte[] buffer = new byte[ChunkSize];
byte[] binary = (datareaderObject["doc"]) as byte[];
MemoryStream ms = new MemoryStream(binary);
int SizeToWrite = ChunkSize;
for (int i = 0; i < binary.GetUpperBound(0) - 1;
i = i + ChunkSize)
{
if (!Response.IsClientConnected) return;
if (i + ChunkSize >= binary.Length)
SizeToWrite = binary.Length - i;
byte[] chunk = new byte[SizeToWrite];
ms.Read(chunk, 0, SizeToWrite);
Response.BinaryWrite(chunk);
Response.Flush();
}
Response.Close();
}
}
if document store in virtual path
if (datareadear.Read())
{
FileInfo file = new
System.IO.FileInfo(Server.MapPath("datareadear["filename"].ToString());
if (file.Exists)
{
Response.Clear();
Response.AddHeader("content-disposition",
"attachment; filename=" + file.Name);
Response.AddHeader("Content-Length",
file.Length.ToString());
Response.ContentType =
"application/octet-stream";
Response.WriteFile(file.FullName);
Response.End();
}
else
{
Response.Redirect("../error.aspx?error=" +
"File dose not exits");
Response.End();
}
}
Is This Answer Correct ? | 2 Yes | 1 No |
Post New Answer View All Answers
Give me some tips in c#?
How we work on N tire architecture in asp.net Please give me Examle...
How to update and insert from datagridview at run time in excel database?
how to insert fname,lname,designation values into database while click on the submit button using windows authentication mode?