public async Task <T> UploadLargeAsync <T>(BasicRawUploadParams parameters, int bufferSize = 20971520) where T : UploadResult, new() { var apiUrlImgUpV = Api.ApiUrlImgUpV; apiUrlImgUpV.ResourceType(Enum.GetName(typeof(ResourceType), parameters.ResourceType).ToLower()); var url = apiUrlImgUpV.BuildUrl(); ResetInternalFileDescription(parameters.File, bufferSize); var extraHeaders = new Dictionary <string, string>(); extraHeaders["X-Unique-Upload-Id"] = RandomPublicId(); parameters.File.BufferLength = bufferSize; var fileLength = parameters.File.GetFileLength(); var obj = default(T); while (!parameters.File.EOF) { var num = Math.Min(bufferSize, fileLength - parameters.File.BytesSent); var paramsDictionary = parameters.ToParamsDictionary(); var str = string.Format("bytes {0}-{1}/{2}", parameters.File.BytesSent, parameters.File.BytesSent + num - 1L, fileLength); extraHeaders["Content-Range"] = str; using (var response = await Api.CallAsync(HttpMethod.Post, url, paramsDictionary, parameters.File, extraHeaders)) { obj = await BaseResult.Parse <T>(response); if (obj.StatusCode != HttpStatusCode.OK) { throw new CloudinaryException($"An error has occured while uploading file (status code: {obj.StatusCode}). {obj.Error?.Message ?? "Unkown error"}"); } } } return(obj); }
/// <summary> /// Uploads large file to cloudinary by dividing it to chunks. /// </summary> /// <param name="parameters">Parameters of file uploading.</param> /// <param name="bufferSize">Chunk (buffer) size (20 MB by default).</param> /// <returns></returns> /// <exception cref="System.ArgumentException"> /// Please use BasicRawUploadParams class for large raw file uploading! /// or /// The UploadLargeRaw method is intended to be used for large local file uploading and can't be used for remote file uploading! /// </exception> public RawUploadResult UploadLargeRaw(BasicRawUploadParams parameters, int bufferSize = 20 * 1024 * 1024) { if (parameters is RawUploadParams) { throw new ArgumentException("Please use BasicRawUploadParams class for large raw file uploading!"); } parameters.Check(); if (parameters.File.IsRemote) { throw new ArgumentException("The UploadLargeRaw method is intended to be used for large local file uploading and can't be used for remote file uploading!"); } string uri = m_api.ApiUrlV.Action("upload_large").ResourceType("raw").BuildUrl(); ResetInternalFileDescription(parameters.File, bufferSize); int partNumber = 1; string publicId = null; string uploadId = null; RawUploadResult result = null; while (!parameters.File.EOF) { var dict = parameters.ToParamsDictionary(); dict.Add("part_number", partNumber); if (partNumber > 1) { dict["public_id"] = publicId; dict["upload_id"] = uploadId; } if (parameters.File.IsLastPart()) { dict["final"] = true; } using (HttpWebResponse response = m_api.Call(HttpMethod.POST, uri, dict, parameters.File)) { var partResult = RawPartUploadResult.Parse(response); result = partResult; if (result.StatusCode != HttpStatusCode.OK) { throw new WebException(String.Format( "An error has occured while uploading file (status code: {0}). {1}", partResult.StatusCode, partResult.Error != null ? partResult.Error.Message : "Unknown error")); } if (partNumber == 1) { publicId = partResult.PublicId; uploadId = partResult.UploadId; } partNumber++; } } return(result); }