Ejemplo n.º 1
0
        public static FileUploadResult UploadFileToFolder(string file, string userEmail, string folder)
        {
            FileUploadResult uploadResult = null;

            try
            {
                HttpClient httpClient = new HttpClient();
                httpClient.DefaultRequestHeaders.Accept.Clear();
                httpClient.DefaultRequestHeaders.Accept.Add(new MediaTypeWithQualityHeaderValue("application/json"));
                // Read the files
                var fileStream = File.Open(file, FileMode.Open);
                var fileInfo   = new FileInfo(file);

                var content = new MultipartFormDataContent();
                content.Add(new StreamContent(fileStream), "\"file\"", string.Format("\"{0}\"", fileInfo.Name)
                            );

                System.Threading.Tasks.Task taskUpload = httpClient.PostAsync(Configuration.GroupDocsAppsAPIBasePath + "api/GroupDocsAppsweb/UploadFileToFolder" + "?userEmail=" + userEmail + "&folder=" + folder, content).ContinueWith(task =>
                {
                    if (task.Status == System.Threading.Tasks.TaskStatus.RanToCompletion)
                    {
                        var response = task.Result;

                        if (response.IsSuccessStatusCode)
                        {
                            uploadResult = response.Content.ReadAsAsync <FileUploadResult>().Result;
                        }
                    }
                    fileStream.Dispose();
                });

                taskUpload.Wait();
                httpClient.Dispose();
                // Delete input file
                if (File.Exists(file))
                {
                    File.Delete(file);
                }
            }
            catch (Exception ex)
            {
                Debug.WriteLine(ex.Message);
            }
            return(uploadResult);
        }
Ejemplo n.º 2
0
        public static FileUploadResult UploadFileToFolder(Stream fileStream, string fileName, string userEmail, string folder)
        {
            FileUploadResult uploadResult = null;

            try
            {
                HttpClient httpClient = new HttpClient();
                httpClient.DefaultRequestHeaders.Accept.Clear();
                httpClient.DefaultRequestHeaders.Accept.Add(new MediaTypeWithQualityHeaderValue("application/json"));

                var content = new MultipartFormDataContent();
                content.Add(new StreamContent(fileStream), "\"file\"", string.Format("\"{0}\"", fileName)
                            );

                System.Threading.Tasks.Task taskUpload = httpClient.PostAsync(Configuration.GroupDocsAppsAPIBasePath + "api/GroupDocsAppsweb/UploadFileToFolder" + "?userEmail=" + userEmail + "&folder=" + folder, content).ContinueWith(task =>
                {
                    if (task.Status == System.Threading.Tasks.TaskStatus.RanToCompletion)
                    {
                        var response = task.Result;

                        if (response.IsSuccessStatusCode)
                        {
                            uploadResult = response.Content.ReadAsAsync <FileUploadResult>().Result;
                        }
                    }
                });

                try
                {
                    taskUpload.Wait((Configuration.ThreadAbortSeconds + 4) * 1000, new CancellationToken(false));
                }
                catch (Exception exc)
                {
                    httpClient.Dispose();
                    throw exc;                    // new Exception("We regret to inform you that your file took more than expected time. We cannot process it at the moment.");
                }
            }
            catch (Exception ex)
            {
                Debug.WriteLine(ex.Message);
            }
            return(uploadResult);
        }