From ce6b232b19318d38c4c23001d31e605a815ab186 Mon Sep 17 00:00:00 2001 From: faufab08 Date: Wed, 13 Nov 2013 07:03:24 +0100 Subject: [PATCH] Update RestRequest.cs Add a new method to add bytes as it was a file of specific content type. This is useful to send a gzipped data on multipart request without the use of temporary file --- RestSharp/RestRequest.cs | 31 +++++++++++++++++++++++++++++++ 1 file changed, 31 insertions(+) diff --git a/RestSharp/RestRequest.cs b/RestSharp/RestRequest.cs index 758fb98ff..c0354657b 100644 --- a/RestSharp/RestRequest.cs +++ b/RestSharp/RestRequest.cs @@ -199,6 +199,37 @@ public IRestRequest AddFile (string name, Action writer, string fileName return AddFile(new FileParameter { Name = name, Writer = writer, FileName = fileName, ContentType = contentType }); } + + /// + /// Add bytes to the Files collection as if it was a file of specific type + /// + /// A form parameter name + /// The file data + /// The file name to use for the uploaded file + /// Specific content type. Es: application/x-gzip + /// + public IRestRequest AddBytesAs(string name, byte[] bytes, string filename, string contentType = "application/x-gzip") + { + + 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);