how to retrive file ,using file info on click event of a
buton and disply it on a web form



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

Post New Answer

More ADO.NET Code Interview Questions

Give me some tips in c#?

0 Answers  


How to update and insert from datagridview at run time in excel database?

0 Answers  


how to connect bind a control to database by writing a stored procedure?

3 Answers   Satyam,


How to connect c# visual studio 2008 to Oracle with ADO.NET? thanks for the answer.

1 Answers  


how to insert fname,lname,designation values into database while click on the submit button using windows authentication mode?

0 Answers  






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?

1 Answers  


how to retrive file ,using file info on click event of a buton and disply it on a web form

1 Answers   Activa Softec,


How we work on N tire architecture in asp.net Please give me Examle...

3 Answers   ABC, HCL, IBM,


Categories
  • ASP.NET Code Interview Questions ASP.NET Code (46)
  • VB.NET Code Interview Questions VB.NET Code (9)
  • C Sharp Code Interview Questions C Sharp Code (51)
  • ADO.NET Code Interview Questions ADO.NET Code (8)