i am using WCF to upload and download a file. and i am using the following the for download.
try
{
MyService.IWITSService clientDownload = new WITSServiceClient();
MyService.DownloadRequest requestData = new DownloadRequest();
MyService.RemoteFileInfo fileInfo = new RemoteFileInfo();
requestData.ItemID = Convert.ToInt32(Request.QueryString["id"]);
fileInfo = clientDownload.DownloadFile(requestData);
Response.BufferOutput = false; // to prevent buffering
byte[] buffer = new byte[6500000];
int bytesRead = 0;
HttpContext.Current.Response.Clear();
HttpContext.Current.Response.ClearHeaders();
HttpContext.Current.Response.ContentType = fileInfo.FileExt;
HttpContext.Current.Response.AddHeader("Content-Disposition","attachment; filename=" + fileInfo.FileName);
bytesRead = fileInfo.FileByteStream.Read(buffer, 0, buffer.Length);
while (bytesRead > 0)
{
// Verify that the client is connected.
if (Response.IsClientConnected)
{
Response.OutputStream.Write(buffer, 0, bytesRead);
// Flush the data to the HTML output.
Response.Flush();
buffer = new byte[6500000];
bytesRead = fileInfo.FileByteStream.Read(buffer, 0, buffer.Length);
}
else
{
bytesRead = -1;
}
}
}
catch (Exception ex)
{
// Trap the error, if any.
System.Web.HttpContext.Current.Response.Write("Error : " + ex.Message);
}
finally
{
Response.Flush();
Response.Close();
Response.End();
System.Web.HttpContext.Current.Response.Close();
}
file is downloaded but file data is not display. file size is same as an actual
size. can any one help me where i have to change..
Thanks in advance..
You don’t want to call neither
Response.Close()norSystem.Web.HttpContext.Current.Response.Close(). It interrupts the connection to the client.The documentation of HttpResponse.Close Method says:
So reduce the
finallycode to: