/// <summary>
 /// Asynchronously downloads file from the specified url to the specified path
 /// </summary>
 /// <param name="url"></param>
 /// <param name="filePath"></param>
 /// <param name="callback">A <see cref="DownloadImageCompletedCallback"/> to be invoked when the request is completed</param>
 /// <param name="progressCallback"></param>
 /// <returns></returns>
 public object DownloadFileAsync(string url, string filePath, DownloadFileCompletedCallback callback, DownloadProgressChangedCallback progressCallback)
 {
     string urlValidationError;
     if (!IsValidURL(url, out urlValidationError))
         throw new ArgumentException(urlValidationError);
     FileRequestAsyncState state = new FileRequestAsyncState(filePath, callback, progressCallback, DownloadFileAsyncCompleted);
     HttpWebServiceRequest request = GetRequest();
     request.GetResponseAsync(url, new FileStream(filePath, FileMode.Create, FileAccess.Write, FileShare.None), IMAGE_ACCEPT, null, state);
     return request;
 }
Ejemplo n.º 2
0
        /// <summary>
        /// Asynchronously downloads file from the specified url to the specified path
        /// </summary>
        /// <param name="url"></param>
        /// <param name="filePath"></param>
        /// <param name="callback">A <see cref="DownloadImageCompletedCallback"/> to be invoked when the request is completed</param>
        /// <param name="progressCallback"></param>
        /// <returns></returns>
        public object DownloadFileAsync(string url, string filePath, DownloadFileCompletedCallback callback, DownloadProgressChangedCallback progressCallback)
        {
            string urlValidationError;

            if (!IsValidURL(url, out urlValidationError))
            {
                throw new ArgumentException(urlValidationError);
            }
            FileRequestAsyncState state   = new FileRequestAsyncState(filePath, callback, progressCallback, DownloadFileAsyncCompleted);
            HttpWebServiceRequest request = GetRequest();

            request.GetResponseAsync(url, new FileStream(filePath, FileMode.Create, FileAccess.Write, FileShare.None), IMAGE_ACCEPT, null, state);
            return(request);
        }
Ejemplo n.º 3
0
        /// <summary>
        /// Callback method for asynchronous requests
        /// </summary>
        private void DownloadFileAsyncCompleted(WebRequestAsyncState state)
        {
            FileRequestAsyncState requestState = (FileRequestAsyncState)state;
            FileInfo fileResult = null;

            if (!requestState.Request.Cancelled && requestState.Error == null)
            {
                try
                {
                    fileResult = new FileInfo(requestState.FilePath);
                }
                catch (Exception ex)
                {
                    requestState.Error = HttpWebServiceException.FileError(requestState.Request.BaseUrl, ex);
                }
            }
            if (requestState.Request.ResponseStream != null)
            {
                requestState.Request.ResponseStream.Close();
            }
            requestState.DownloadFileCompleted(new DownloadFileAsyncResult(fileResult, requestState.Error, requestState.Request.Cancelled));
        }