R
Rick
The following code download the file but it does not bring the file Open/Save
dialog box. How do I fix the code so it will bring up the Open/Save dialog
box?
public static int DownloadFile()
{
int bytesProcessed = 0;
Stream remoteStream = null;
Stream localStream = null;
WebResponse response = null;
string remoteFilename = "http://MyServer/StoredFiles/MyFile.xls";
localFilename = @"c:\files\dMyFile.xls";
try
{
WebRequest request = WebRequest.Create(remoteFilename);
if (request != null)
{
response = request.GetResponse();
if (response != null)
{
remoteStream = response.GetResponseStream();
localStream = File.Create(localFilename);
byte[] buffer = new byte[1024];
int bytesRead;
response.ContentType = "application/vnd.ms-excel";
response.Headers.Add("Content-Disposition",
"attachment; filename=" + localFilename)
do
{
bytesRead = remoteStream.Read(buffer, 0,
buffer.Length);
localStream.Write(buffer, 0, bytesRead);
bytesProcessed += bytesRead;
} while (bytesRead > 0);
}
}
}
catch (Exception e)
{
Console.WriteLine(e.Message);
}
finally
{
if (response != null) response.Close();
if (remoteStream != null) remoteStream.Close();
if (localStream != null) localStream.Close();
}
return bytesProcessed;
}
dialog box. How do I fix the code so it will bring up the Open/Save dialog
box?
public static int DownloadFile()
{
int bytesProcessed = 0;
Stream remoteStream = null;
Stream localStream = null;
WebResponse response = null;
string remoteFilename = "http://MyServer/StoredFiles/MyFile.xls";
localFilename = @"c:\files\dMyFile.xls";
try
{
WebRequest request = WebRequest.Create(remoteFilename);
if (request != null)
{
response = request.GetResponse();
if (response != null)
{
remoteStream = response.GetResponseStream();
localStream = File.Create(localFilename);
byte[] buffer = new byte[1024];
int bytesRead;
response.ContentType = "application/vnd.ms-excel";
response.Headers.Add("Content-Disposition",
"attachment; filename=" + localFilename)
do
{
bytesRead = remoteStream.Read(buffer, 0,
buffer.Length);
localStream.Write(buffer, 0, bytesRead);
bytesProcessed += bytesRead;
} while (bytesRead > 0);
}
}
}
catch (Exception e)
{
Console.WriteLine(e.Message);
}
finally
{
if (response != null) response.Close();
if (remoteStream != null) remoteStream.Close();
if (localStream != null) localStream.Close();
}
return bytesProcessed;
}