Beispiel #1
0
        /// <summary>
        /// download file and save it
        /// </summary>
        /// <param name="url"></param>
        /// <param name="fullPath">file's full path to save</param>
        /// <returns></returns>
        public static async System.Threading.Tasks.Task <NetResponse> DownloadFileAsync(string url, string fullPath)
        {
            //httpClient.DefaultRequestHeaders.Accept.Clear();
            var result = await httpClient.GetAsync(url);

            var response = new NetResponse();

            try
            {
                if (!result.IsSuccessStatusCode)
                {
                    response.Successful = false;
                    response.Message    = await result.Content.ReadAsStringAsync();

                    return(response);
                }
                string folder = Path.GetDirectoryName(fullPath);
                if (!string.IsNullOrWhiteSpace(folder))
                {
                    if (!Directory.Exists(folder))
                    {
                        Directory.CreateDirectory(folder);
                    }
                }
                File.WriteAllBytes(fullPath, await result.Content.ReadAsByteArrayAsync());

                response.Successful = true;
            }
            catch (Exception ex)
            {
                response.Successful = false;
                response.Message    = ex.Message;
            }

            return(response);
        }