Example #1
0
 /// <summary>
 /// Асинхронная загрузка файлов созданием нового таска
 /// </summary>
 /// <param name="dataInfo"></param>
 /// <returns></returns>
 public async Task<UploadResult> UploadingAsync(DataInfo dataInfo)
 {
     try
     {
         // функция загрузки
         Func<UploadResult> uploadFunc = new Func<UploadResult>(() => UploadFiles(dataInfo));
         // создание таска фабрикой
         Task<UploadResult> returnTaskObject = Task.Factory.StartNew<UploadResult>(uploadFunc);
         // асинхронный запуск таска с результатом
         var result = await returnTaskObject;
         return result;
     }
     catch (Exception)
     {
         throw new FaultException<UploadFault>(new UploadFault { Message = "Ошибка загрузки" });
     }
 }
Example #2
0
        /// <summary>
        /// Асинхронный метод старта з агрузки
        /// </summary>
        /// <param name="dataInfo"></param>
        /// <param name="callback"></param>
        /// <param name="state"></param>
        /// <returns></returns>
        public IAsyncResult BeginUploading(DataInfo dataInfo, AsyncCallback callback, object state)
        {
            // коллбэк завершения
            var tcs = new TaskCompletionSource<UploadResult>(state);
            var task = UploadingAsync(dataInfo);
            task.ContinueWith(t =>
            {
                if (t.IsFaulted)
                    tcs.TrySetException(t.Exception.InnerExceptions);
                else if (t.IsCanceled)
                    tcs.TrySetCanceled();
                else
                    tcs.TrySetResult(t.Result);

                if (callback != null)
                    callback(tcs.Task);
            });

            return tcs.Task;
        }
Example #3
0
        /// <summary>
        /// Метод, загружающий файлы
        /// </summary>
        /// <param name="dataInfo"></param>
        /// <returns></returns>
        private UploadResult UploadFiles(DataInfo dataInfo)
        {
            UploadResult result = new UploadResult();

            // TODO: вынести в настройки
            string FilePath = Path.Combine("d:/Files/2015/", dataInfo.FileName);

            int length = 0;
            using (FileStream writer = new FileStream(FilePath, FileMode.Create))
            {
                int readCount;
                var buffer = new byte[8192];
                while ((readCount = dataInfo.Stream.Read(buffer, 0, buffer.Length)) != 0)
                {
                    writer.Write(buffer, 0, readCount);
                    length += readCount;
                }
            }

            return result;
        }