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

  • Question

  • 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.

    Tuesday, July 10, 2018 5:21 AM

All replies

  • What is the value of your "remoteUri" and "fn"?

    Also, do you have "Content-Type" header in the response? With it you would be able to obtain/create the file's extension and then you could name the file however you want.

    For example something like this:

    var request = (HttpWebRequest)HttpWebRequest.Create(remoteUri);
    using (var response = (HttpWebResponse)request.GetResponse())
    {
        string extension;
        switch(response.Headers["Content-Type"])
        {
            case "application/vnd.ms-excel":
                extension = ".xls";
                break;
            case "application/vnd.openxmlformats-officedocument.spreadsheetml.sheet":
                extension = ".xlsx";
                break;
            default:
                extension = "";
                break;
        }
    
        string fn = Path.Combine(@"E:\ABC\ConsoleApplication1\ConsoleApplication1\Downloaded",
            Path.GetRandomFileName() + extension);
    
        using (var responseStream = response.GetResponseStream())
        using (var fileStream = File.Create(fn))
        {
            responseStream.CopyTo(fileStream);
            Console.Write("DOWNLOADING.....");
        }
    }
    
    Console.Write("DOWNLOADED");

    Or perhaps you could name the file based on "remoteUri".

    Tuesday, July 10, 2018 7:22 AM
  • Hi JackLPonce,

    I got the Issue halfway solved, the error was with the path it had double quotes so had to replace them with the single quotes to get it right.

    Now the only problem is it is supposed to download 4 files together mentioned in the excel based on the url but only 2 is getting downloaded. the other 2 file which are not getting downloaded have an extension of pdf and docx.
    And also it's throwing an error :- "object reference not set to an instance of an object"

    Thanks Much Appreciated.

     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();
                                    string filename=Path.Combine(basePath, fn.Replace('"',' '));
                                    using (var fileStream = File.Create(filename))
                                    {
                                        responseStream.CopyTo(fileStream);
                                        Console.Write("DOWNLOADING.....");
                                    }
                                }
    
                                Console.Write("DOWNLOADED");
                            }
                            catch(Exception ex)
                            {
                                //Console.WriteLine(ex.Message);
                                continue;
                            }
                        }


    • Edited by Hani2693 Tuesday, July 10, 2018 10:13 AM
    Tuesday, July 10, 2018 8:58 AM