When I download a file using WebClient. It Downloaded the first time but timeout exception happening on the second download.
The File name is in the Header as contentDisposition
It took by using webClient.OpenRead(filePath);
then save with this name. Below shown the using code
using (WebClient webClient = new WebClient())
{
try
{
webClient.OpenRead(filePath);
string cpString = webClient.ResponseHeaders["Content-Disposition"];
ContentDisposition contentDisposition = new ContentDisposition(cpString);
string filename = contentDisposition.FileName;
localPath = Path.Combine(fullpath, filename);
if (File.Exists(localPath))
{
int count = 1;
string fileNameOnly = Path.GetFileNameWithoutExtension(localPath);
string extension = Path.GetExtension(localPath);
string path = Path.GetDirectoryName(localPath);
string newlocalPath = localPath;
while (File.Exists(newFullPath))
{
string tempFileName = string.Format("{0}({1})", fileNameOnly, count++);
newlocalPath= Path.Combine(path, tempFileName + extension);
}
webClient.DownloadFile(filePath, newlocalPath);
}
else webClient.DownloadFile(filePath, localPath);
}
catch (Exception)
{
throw;
}
finally
{
if (webClient != null)
{
webClient.Dispose();
}
}
}
Is it a proper way? I would appreciate it if you suggest a solution for this.