How can i download a file without file name and file extension in the url C# console Application?

  • Thread starter Thread starter Hani2693
  • Start date Start date
H

Hani2693

Guest
if (i > 1 && xlRange.Cells[1,j].Value2.ToString()=="Url" && xlRange.Cells[i, j].Value2 != null)
{
try
{
string remoteUri = xlRange.Cells[i, j].Value2.ToString();


HttpWebRequest request = (HttpWebRequest)HttpWebRequest.Create(remoteUri);
using (HttpWebResponse response = (HttpWebResponse)request.GetResponse())
{
var fn = response.Headers["Content-Disposition"].Split(new string[] { "=" }, StringSplitOptions.None)[1];
string basePath = @"E:\ABC\ConsoleApplication1\ConsoleApplication1\Downloaded"; // Change accordingly...
var responseStream = response.GetResponseStream();
using (var fileStream = File.Create(Path.Combine(basePath, fn)))
{
responseStream.CopyTo(fileStream);
Console.Write("DOWNLOADING.....");
}
}

Console.Write("DOWNLOADED");
}
catch(Exception ex)
{
Console.WriteLine(ex.Message);
continue;
}
}
}

I want my program to read the excel sheet based on header value specified in the sheet. I've a column named "URL" so i want to read Based on the "URL" and then it should be able download file while reading that particular column.


The problem is with the download actually, Since i've just specified the directory it is not working that way, So would require an alternative to use instead of a fileName.

And i'm getting this error now:- "Illegal characters in path."


Thanks.

Continue reading...
 
Back
Top