/// <summary>Downloads the resource with the specified URI as a byte array, asynchronously.</summary>
 /// <param name="webClient">The WebClient.</param>
 /// <param name="address">The URI from which to download data.</param>
 /// <returns>A Task that contains the downloaded data.</returns>
 public static Task <byte[]> DownloadDataTask(this WebClient webClient, string address)
 {
     return(webClient.DownloadDataTask(new Uri(address)));
 }
Example #2
0
        /// <summary>
        /// 지정된 경로에서 리소스를 읽어오는 Task{byte[]}를 빌드합니다.
        /// </summary>
        private static Task<byte[]> ReadFileTask(HttpContext context, string virtualPath) {
            virtualPath.ShouldNotBeWhiteSpace("virtualPath");

            Task<byte[]> task = null;

            if(IsDebugEnabled)
                log.Debug("지정된 경로에서 리소스를 읽어오는 Task<byte[]>를 빌드합니다. virtualPath=[{0}]", virtualPath);

            // 외부경로의 리소스라면, 다운로드 받는다.
            if(IsWebResource(virtualPath)) {
                var webClient = new WebClient();

                task = webClient.DownloadDataTask(virtualPath);
                task.ContinueWith(_ => webClient.Dispose(), TaskContinuationOptions.ExecuteSynchronously);
            }
            else {
                var path = FileTool.GetPhysicalPath(virtualPath);

                task = path.FileExists()
                           ? FileAsync.ReadAllBytes(FileTool.GetPhysicalPath(virtualPath))
                           : Task.Factory.FromResult(new byte[0]);
            }

            return task;
        }