Skip to content
Closed
Changes from all commits
Commits
File filter

Filter by extension

Filter by extension

Conversations
Failed to load comments.
Loading
Jump to
Jump to file
Failed to load files.
Loading
Diff view
Diff view
31 changes: 31 additions & 0 deletions RestSharp/RestRequest.cs
Original file line number Diff line number Diff line change
Expand Up @@ -199,6 +199,37 @@ public IRestRequest AddFile (string name, Action<Stream> writer, string fileName
return AddFile(new FileParameter { Name = name, Writer = writer, FileName = fileName, ContentType = contentType });
}


/// <summary>
/// Add bytes to the Files collection as if it was a file of specific type
/// </summary>
/// <param name="name">A form parameter name</param>
/// <param name="bytes">The file data</param>
/// <param name="filename">The file name to use for the uploaded file</param>
/// <param name="contentType">Specific content type. Es: application/x-gzip </param>
/// <returns></returns>
public IRestRequest AddBytesAs(string name, byte[] bytes, string filename, string contentType = "application/x-gzip")
Copy link
Contributor

Choose a reason for hiding this comment

The reason will be displayed to describe this comment to others. Learn more.

Could you validate these arguments? For example, I assume name cannot be null or empty. bytes shouldn't be null either. Throw an ArgumentException or ArgumentNullException where appropriate.

{

Copy link
Contributor

Choose a reason for hiding this comment

The reason will be displayed to describe this comment to others. Learn more.

Thanks for submitting a PR! Please fix up the white space. RestSharp uses tabs, not spaces. Also, this new line here is unnecessary.

long length = bytes.Length;
return AddFile(new FileParameter
{
Name = name,
FileName = filename,
ContentLength = length,
ContentType = contentType,
Writer = s =>
{
using (var file = new StreamReader(new MemoryStream(bytes)))
{
file.BaseStream.CopyTo(s);
}
}
});
}



private IRestRequest AddFile (FileParameter file)
{
Files.Add(file);
Expand Down