1

My problem is that I dont Know how i can download a File withknowing the file name or the file extension in the url, like this http://findicons.com/icon/download/235456/internet_download/128/png?id=235724

I hope you can help me

  • do you think share your example code ? – Ivan Barayev May 14 '16 at 15:24
  • 1
    It is up to the webserver what result it will send. When it is a file, the server will let the client know what the content and name of the file is. You have to send a request to determine what the extension is. There is no way of knowing without communicating. – Laurence May 14 '16 at 15:25
  • What i want is to download the file without knowing the name and the extension of it into Folder. –  May 14 '16 at 15:35
  • @HerrJava So will the url always contain the filename and extension as in this case? – Fᴀʀʜᴀɴ Aɴᴀᴍ May 14 '16 at 15:47

3 Answers3

3

You could inspect the Content-Disposition response header using an HTTP request to get the filename. This would be a more general solution, so even if the filename is not contained in the URL, it would work:

var url = "http://findicons.com/icon/download/235456/internet_download/128/png?id=235724";
HttpWebRequest request = (HttpWebRequest)HttpWebRequest.Create(url);
using (HttpWebResponse response = (HttpWebResponse)request.GetResponse())
{
    var fn = response.Headers["Content-Disposition"].Split(new string[] { "=" }, StringSplitOptions.None)[1];
    string basePath = @"X:\Folder\SubFolder"; // Change accordingly...
    var responseStream = response.GetResponseStream();
    using (var fileStream = File.Create(Path.Combine(basePath, fn)))
    {
        responseStream.CopyTo(fileStream);
    }
}

The above code uses certain methods and functions, you can find more information here:


Hope this answer helps you :)

Community
  • 1
  • 1
Fᴀʀʜᴀɴ Aɴᴀᴍ
  • 6,131
  • 5
  • 31
  • 52
3

It's possible to get the filename since the server is sending the Content-Disposition header. Here's a code example on how to get the filename using the HttpClient class:

var url = "http://findicons.com/icon/download/235456/internet_download/128/png?id=235724";

using (var client = new HttpClient())
using (var response = await client.GetAsync(url))
{
    // make sure our request was successful
    response.EnsureSuccessStatusCode();

    // read the filename from the Content-Disposition header
    var filename = response.Content.Headers.ContentDisposition.FileName;

    // read the downloaded file data
    var stream = await response.Content.ReadAsStreamAsync();

    // Where you want the file to be saved
    var destinationFile = Path.Combine("C:\\local\\directory", filename);

    // write the steam content into a file
    using (var fileStream = File.Create(destinationFile))
    {
        stream.CopyTo(fileStream);
    }
}
Nasreddine
  • 36,610
  • 17
  • 75
  • 94
  • @HerrJava glad it solved your problem. Don't forget to upvote and mark the answer as accepted to close this topic. Happy coding :) – Nasreddine May 14 '16 at 16:38
0

I've had a hard time solving this issue myself and have come to a solution solving some issues in relation of obtaining the file name automatically.

Some headers does not include the Content Disposition as suggested in https://stackoverflow.com/a/37228939/8805908, but the ones that do is still used.

I was wondering how Chrome, firefox etc. could obtain the name of the file though this information is not available through any header entry. I found that the links left without the information could be derived via its URL, from which i am using the code of:

http://codesnippets.fesslersoft.de/how-to-get-the-filename-of-url-in-c-and-vb-net/

My conclusion so far is; check the header for Content Disposition, if this does not contain any information check the URL for any file matches. So far I have not found a download link that I have not been able to retrieve the file name of.

I hope this may solve some issues.

--- Edit 12-06-2018

The solution using these methods are satisfying the following links: 5 test cases