user2376998
user2376998

Reputation: 1071

Using webclient to download images from deployed website

i deployed a website on IIS running on localhost/xxx/xxx.aspx . On my WPF side , i download a textfile using webclient from the localhost server and save it at my wpf app folder this is how i do it :

  protected void DownloadData(string strFileUrlToDownload)
    {
        WebClient client = new WebClient();
        byte[] myDataBuffer = client.DownloadData(strFileUrlToDownload);         

        MemoryStream storeStream = new MemoryStream();

        storeStream.SetLength(myDataBuffer.Length);
        storeStream.Write(myDataBuffer, 0 , (int)storeStream.Length);

        storeStream.Flush();

        string currentpath = System.IO.Directory.GetCurrentDirectory() + @"\Folder";

        using (FileStream file = new FileStream(currentpath, FileMode.Create, System.IO.FileAccess.ReadWrite))
        {
            byte[] bytes = new byte[storeStream.Length];
            storeStream.Read(bytes, 0, (int)storeStream.Length);
            file.Write(myDataBuffer, 0, (int)storeStream.Length);
            storeStream.Close();
        }

        //The below Getstring method to get data in raw format and manipulate it as per requirement
        string download = Encoding.ASCII.GetString(myDataBuffer);


    }

This is by writing btyes and saving them . But how do i download multiple image files and save it on my WPF app folder? I have a URL like this localhost/websitename/folder/designs/ , under this URL , there is many images , how do i download all of them ? and save it on WPF app folder?

Basically i want to download the contents of the folder whereby the contents are actually images.

Upvotes: 1

Views: 3164

Answers (1)

Dark Flame Master
Dark Flame Master

Reputation: 26

First, the WebClient class already has a method for this. Use something like client.DownloadFile(remoteUrl, localFilePath).

See this link:

WebClient.DownloadFile Method (String, String)

Secondly, you will need to index the files you want to download on the server somehow. You can't just get a directory listing over HTTP and then loop through it. The web server will need to be configured to enable directory listing, or you will need a page to generate a directory listing. Then you will need to download the results of that page as a string using WebClient.DownloadString and parse it. A simple solution would be an aspx page that outputs a plaintext list of files in the directory you want to download.

Finally, in the code you posted you're saving every single file you download as a file named "Folder". You need to generate a unique filename for each file you want to download. When you're looping through the files you want to download, use something like:

string localFilePath = Path.Combine("MyDownloadFolder", imageName);

where imageName is a unique filename (with file extension) for that file.

Upvotes: 1

Related Questions