/// <summary> /// Uploads a provided file to the target parent folder. /// If the file already exists, an error will be thrown. /// A proper timeout should be provided for large uploads. /// </summary> /// <param name="fileRequest">BoxFileRequest object.</param> /// <param name="stream">Stream of uploading file.</param> /// <param name="fields">Fields which shall be returned in result.</param> /// <param name="timeout">Timeout for response.</param> /// <param name="contentMD5">The SHA1 hash of the file.</param> /// <param name="setStreamPositionToZero">Set position for input stream to 0.</param> /// <param name="uploadUri">Uri to use for upload. Default upload endpoint URI is used if not specified.</param> /// <returns>A full file object is returned inside of a collection if the ID is valid and if the update is successful.</returns> public async Task <BoxFile> UploadAsync(BoxFileRequest fileRequest, Stream stream, List <string> fields = null, TimeSpan?timeout = null, byte[] contentMD5 = null, bool setStreamPositionToZero = true, Uri uploadUri = null) { stream.ThrowIfNull("stream"); fileRequest.ThrowIfNull("fileRequest") .Name.ThrowIfNullOrWhiteSpace("filedRequest.Name"); fileRequest.Parent.ThrowIfNull("fileRequest.Parent") .Id.ThrowIfNullOrWhiteSpace("fileRequest.Parent.Id"); if (setStreamPositionToZero) { stream.Position = 0; } uploadUri = uploadUri == null ? _config.FilesUploadEndpointUri : uploadUri; BoxMultiPartRequest request = new BoxMultiPartRequest(uploadUri) { Timeout = timeout } .Param(ParamFields, fields) .FormPart(new BoxStringFormPart() { Name = "attributes", Value = _converter.Serialize(fileRequest) }) .FormPart(new BoxFileFormPart() { Name = "file", Value = stream, FileName = fileRequest.Name }); if (contentMD5 != null) { request.Header(Constants.RequestParameters.ContentMD5, HexStringFromBytes(contentMD5)); } IBoxResponse <BoxCollection <BoxFile> > response = await ToResponseAsync <BoxCollection <BoxFile> >(request).ConfigureAwait(false); // We can only upload one file at a time, so return the first entry return(response.ResponseObject.Entries.FirstOrDefault()); }
/// <summary> /// This method is used to upload a new version of an existing file in a user’s account. Similar to regular file uploads, /// these are performed as multipart form uploads. An optional If-Match header can be included to ensure that client only /// overwrites the file if it knows about the latest version. The filename on Box will remain the same as the previous version. /// To update the file’s name, you can specify a new name for the file using the fileName parameter. /// A proper timeout should be provided for large uploads. /// </summary> /// <param name="fileName">Name of the file.</param> /// <param name="fileId">Id of the file to upload a new version to.</param> /// <param name="stream">Stream of the uploading file.</param> /// <param name="etag">This ‘etag’ field of the file, which will be set in the If-Match header.</param> /// <param name="fields">Fields which shall be returned in result.</param> /// <param name="timeout">Optional timeout for response.</param> /// <param name="contentMD5">The SHA1 hash of the file.</param> /// <param name="setStreamPositionToZero">Set position for input stream to 0.</param> /// <param name="uploadUri">Optional url for uploading file.</param> /// <returns>A full file object is returned.</returns> public async Task <BoxFile> UploadNewVersionAsync(string fileName, string fileId, Stream stream, string etag = null, List <string> fields = null, TimeSpan?timeout = null, byte[] contentMD5 = null, bool setStreamPositionToZero = true, Uri uploadUri = null) { fileName.ThrowIfNullOrWhiteSpace("fileName"); fileId.ThrowIfNullOrWhiteSpace("fileId"); stream.ThrowIfNull("stream"); if (setStreamPositionToZero) { stream.Position = 0; } uploadUri = uploadUri == null ? new Uri(string.Format(Constants.FilesNewVersionEndpointString, fileId)) : uploadUri; BoxMultiPartRequest request = new BoxMultiPartRequest(uploadUri) { Timeout = timeout } .Header(Constants.RequestParameters.IfMatch, etag) .Param(ParamFields, fields) .FormPart(new BoxFileFormPart() { Name = "filename", Value = stream, FileName = fileName }); if (contentMD5 != null) { request.Header(Constants.RequestParameters.ContentMD5, HexStringFromBytes(contentMD5)); } IBoxResponse <BoxCollection <BoxFile> > response = await ToResponseAsync <BoxCollection <BoxFile> >(request).ConfigureAwait(false); // We can only upload one file at a time, so return the first entry return(response.ResponseObject.Entries.FirstOrDefault()); }
/// <summary> /// This method is used to upload a new version of an existing file in a user’s account. Similar to regular file uploads, /// these are performed as multipart form uploads. An optional If-Match header can be included to ensure that client only /// overwrites the file if it knows about the latest version. The filename on Box will remain the same as the previous version. /// To update the file’s name, you can specify a new name for the file using the fileName parameter. /// A proper timeout should be provided for large uploads. /// </summary> /// <param name="fileName">Name of the file.</param> /// <param name="fileId">Id of the file to upload a new version to.</param> /// <param name="stream">Stream of the uploading file.</param> /// <param name="etag">This ‘etag’ field of the file, which will be set in the If-Match header.</param> /// <param name="fields">Fields which shall be returned in result.</param> /// <param name="timeout">Optional timeout for response.</param> /// <param name="contentMD5">The SHA1 hash of the file.</param> /// <param name="setStreamPositionToZero">Set position for input stream to 0.</param> /// <param name="uploadUri">Optional url for uploading file.</param> /// <returns>A full file object is returned.</returns> public async Task<BoxFile> UploadNewVersionAsync(string fileName, string fileId, Stream stream, string etag = null, List<string> fields = null, TimeSpan? timeout = null, byte[] contentMD5 = null, bool setStreamPositionToZero = true, Uri uploadUri = null) { fileName.ThrowIfNullOrWhiteSpace("fileName"); fileId.ThrowIfNullOrWhiteSpace("fileId"); stream.ThrowIfNull("stream"); if (setStreamPositionToZero) stream.Position = 0; uploadUri = uploadUri == null ? new Uri(string.Format(Constants.FilesNewVersionEndpointString, fileId)) : uploadUri; BoxMultiPartRequest request = new BoxMultiPartRequest(uploadUri) { Timeout = timeout } .Header(Constants.RequestParameters.IfMatch, etag) .Param(ParamFields, fields) .FormPart(new BoxFileFormPart() { Name = "filename", Value = stream, FileName = fileName }); if (contentMD5 != null) request.Header(Constants.RequestParameters.ContentMD5, HexStringFromBytes(contentMD5)); IBoxResponse<BoxCollection<BoxFile>> response = await ToResponseAsync<BoxCollection<BoxFile>>(request).ConfigureAwait(false); // We can only upload one file at a time, so return the first entry return response.ResponseObject.Entries.FirstOrDefault(); }
/// <summary> /// Uploads a provided file to the target parent folder. /// If the file already exists, an error will be thrown. /// A proper timeout should be provided for large uploads. /// </summary> /// <param name="fileRequest">BoxFileRequest object.</param> /// <param name="stream">Stream of uploading file.</param> /// <param name="fields">Fields which shall be returned in result.</param> /// <param name="timeout">Timeout for response.</param> /// <param name="contentMD5">The SHA1 hash of the file.</param> /// <param name="setStreamPositionToZero">Set position for input stream to 0.</param> /// <param name="uploadUri">Uri to use for upload. Default upload endpoint URI is used if not specified.</param> /// <returns>A full file object is returned inside of a collection if the ID is valid and if the update is successful.</returns> public async Task<BoxFile> UploadAsync(BoxFileRequest fileRequest, Stream stream, List<string> fields = null, TimeSpan? timeout = null, byte[] contentMD5 = null, bool setStreamPositionToZero = true, Uri uploadUri = null) { stream.ThrowIfNull("stream"); fileRequest.ThrowIfNull("fileRequest") .Name.ThrowIfNullOrWhiteSpace("filedRequest.Name"); fileRequest.Parent.ThrowIfNull("fileRequest.Parent") .Id.ThrowIfNullOrWhiteSpace("fileRequest.Parent.Id"); if (setStreamPositionToZero) stream.Position = 0; uploadUri = uploadUri == null ? _config.FilesUploadEndpointUri : uploadUri; BoxMultiPartRequest request = new BoxMultiPartRequest(uploadUri) { Timeout = timeout } .Param(ParamFields, fields) .FormPart(new BoxStringFormPart() { Name = "attributes", Value = _converter.Serialize(fileRequest) }) .FormPart(new BoxFileFormPart() { Name = "file", Value = stream, FileName = fileRequest.Name }); if (contentMD5 != null) request.Header(Constants.RequestParameters.ContentMD5, HexStringFromBytes(contentMD5)); IBoxResponse<BoxCollection<BoxFile>> response = await ToResponseAsync<BoxCollection<BoxFile>>(request).ConfigureAwait(false); // We can only upload one file at a time, so return the first entry return response.ResponseObject.Entries.FirstOrDefault(); }