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
- The image isnt downloaded (System.IO.File.Exist = False)
- 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