Skip to content

Commit f551d77

Browse files
author
cecil
committed
added IRestRequest AddFile(string name, string path, string contentType)
now it's possible to upload a file with a correct content-type
1 parent 0b6ffe8 commit f551d77

File tree

2 files changed

+39
-0
lines changed

2 files changed

+39
-0
lines changed

RestSharp/IRestRequest.cs

Lines changed: 10 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -139,6 +139,16 @@ public interface IRestRequest
139139
/// <returns>This request</returns>
140140
IRestRequest AddFile(string name, string path);
141141

142+
/// <summary>
143+
/// Adds a file to the Files collection to be included with a POST or PUT request with the specified content type
144+
/// (other methods do not support file uploads).
145+
/// </summary>
146+
/// <param name="name">The parameter name to use in the request</param>
147+
/// <param name="path">Full path to file to upload</param>
148+
/// <param name="contentType">The MIME type of the file to upload</param>
149+
/// <returns>This request</returns>
150+
IRestRequest AddFile(string name, string path, string contentType);
151+
142152
/// <summary>
143153
/// Adds the bytes to the Files collection with the specified file name
144154
/// </summary>

RestSharp/RestRequest.cs

Lines changed: 29 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -141,6 +141,35 @@ public IRestRequest AddFile(string name, string path)
141141
});
142142
}
143143

144+
/// <summary>
145+
/// Adds a file to the Files collection to be included with a POST or PUT request with the specified content type
146+
/// (other methods do not support file uploads).
147+
/// </summary>
148+
/// <param name="name">The parameter name to use in the request</param>
149+
/// <param name="path">Full path to file to upload</param>
150+
/// <param name="contentType">The MIME type of the file to upload</param>
151+
/// <returns>This request</returns>
152+
public IRestRequest AddFile(string name, string path, string contentType)
153+
{
154+
FileInfo f = new FileInfo(path);
155+
long fileLength = f.Length;
156+
157+
return AddFile(new FileParameter
158+
{
159+
Name = name,
160+
FileName = Path.GetFileName(path),
161+
ContentLength = fileLength,
162+
Writer = s =>
163+
{
164+
using (var file = new StreamReader(path))
165+
{
166+
file.BaseStream.CopyTo(s);
167+
}
168+
},
169+
ContentType = contentType
170+
});
171+
}
172+
144173
/// <summary>
145174
/// Adds the bytes to the Files collection with the specified file name
146175
/// </summary>

0 commit comments

Comments
 (0)