private static MemoryStream downloadData(string url)
{
try
{
progress.Value = 0;
Application.DoEvents();
WebRequest req = WebRequest.Create(url);
WebResponse response = req.GetResponse();
Stream stream = response.GetResponseStream();
Byte[] buffer = new Byte[1024];
Int32 dataLength = (Int32)response.ContentLength;
progress.Maximum = dataLength;
Application.DoEvents();
MemoryStream memStream = new MemoryStream();
while (true)
{
Int32 bytesRead = stream.Read(buffer, 0, buffer.Length);
if (bytesRead == 0)
{
progress.Value = progress.Maximum;
Application.DoEvents();
break;
}
else
{
memStream.Write(buffer, 0, bytesRead);
if (progress.Value + bytesRead <= progress.Maximum)
{
progress.Value += bytesRead;
progress.Refresh();
Application.DoEvents();
}
}
}
stream.Close();
return memStream;
}
catch
{
throw new Exception("There was an error accessing the URL.");
}
}
now to use this
string str = @"~/Reports/ReportViewing.aspx?report=rptReservationByOffice&StartDate=" + txtStartDate.Text + "&EndDate=" + txtEndDate.Text;
byte[] DownloadedData = DownloadData(str);
if (DownloadedData == null || DownloadedData.Length == 0)
return;
using (Stream Writer = File.Create(FilePath))
{
Writer.Write(DownloadedData, 0, DownloadedData.Length);
}