1

Im using CopyToAsync to download an image to a local server. After download I have to Upload the file to another webserver using FluentFTP.

My code to download the image is :

public static async Task DownloadImage(Entity.LoginResult loginResult, string saveFolder, string image, string saveFile)
    {
        string filename = System.IO.Path.GetFileNameWithoutExtension(saveFile);
        string extension = System.IO.Path.GetExtension(saveFile);
        {
            saveFile = GetValidUrlString(filename, "-", 255) + extension;
            using (var httpClient = new HttpClient())
            {
                using (var request = new HttpRequestMessage(new HttpMethod("GET"), apiUrl + image + "/download"))
                {
                    request.Headers.TryAddWithoutValidation("Authorization", "CBX-SIMPLE-TOKEN Token=" + loginResult.token);
                    var response = httpClient.SendAsync(request).Result;
                    if (response.StatusCode == System.Net.HttpStatusCode.OK)
                    {
                        using (var fs = new FileStream(saveFolder + saveFile, FileMode.Create))
                        {                                
                            await response.Content.CopyToAsync(fs);
                        }
                    }
                }
            }
        }
    }

After calling DownloadImage i would like to check if the file is downloaded like:

if (System.IO.File.Exists(saveFolder + media.filename))
{ Call ftp  }

I get the following to errors when ftp'ing the images

  1. The image isnt downloaded (System.IO.File.Exist = False)
  2. The image is in progress and FTP fails because the file is in use.

Can anyone tell me how to use async/await so both Download and upload is done

  • what's the value of response.StatusCode ? – auburg Aug 26 '21 at 10:40
  • Ill only progress if its System.Net.HttpStatusCode.OK (if (response.StatusCode == System.Net.HttpStatusCode.OK)) – user2435866 Aug 26 '21 at 11:20
  • See if this helps https://stackoverflow.com/questions/54883668/await-httpcontent-copytoasync-results-in-empty-target-stream – auburg Aug 26 '21 at 11:26
  • It appears you're not awaiting this Task. Why don't you make `DownloadImage` at least return a `bool` value, to signal that it succeeded or failed (hence, change the return type to `Task`). -- Where are you calling this method from? Can you post that code? -- What type of application is this? – Jimi Aug 26 '21 at 11:56
  • 1
    following on from @Jimi's comment you should be doing `var response = await httpClient.SendAsync(request);` – auburg Aug 26 '21 at 12:33

1 Answers1

0
  1. Change line var response = httpClient.SendAsync(request).Result;
    to var response = await httpClient.SendAsync(request); In company i am currently working, .Result in tasks like that was not working properly sometimes.

  2. If downloading fails, throw.

    if (response.StatusCode != System.Net.HttpStatusCode.OK) { throw new Exception("download failed"). }

  3. To ensure that downloading took place, you need to call it with await like this
    await DownloadImage(params)

  4. Good practice is "once async, all async". Meaning when you use one async, all your code should be async.