how to retrive file ,using file info on click event of a
buton and disply it on a web form
Answer / 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 |
Give me some tips in c#?
How to update and insert from datagridview at run time in excel database?
how to connect bind a control to database by writing a stored procedure?
How to connect c# visual studio 2008 to Oracle with ADO.NET? thanks for the answer.
how to insert fname,lname,designation values into database while click on the submit button using windows authentication mode?
In the Design view in Visual Studio 2005 of an ASP.NET web page, what is the easiest way to create an event handler for the default event of an ASP.NET server control?
how to retrive file ,using file info on click event of a buton and disply it on a web form
How we work on N tire architecture in asp.net Please give me Examle...