Skip to content
Merged
Show file tree
Hide file tree
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
10 changes: 10 additions & 0 deletions RestSharp/IRestRequest.cs
Original file line number Diff line number Diff line change
Expand Up @@ -139,6 +139,16 @@ public interface IRestRequest
/// <returns>This request</returns>
IRestRequest AddFile(string name, string path);

/// <summary>
/// Adds a file to the Files collection to be included with a POST or PUT request with the specified content type
/// (other methods do not support file uploads).
/// </summary>
/// <param name="name">The parameter name to use in the request</param>
/// <param name="path">Full path to file to upload</param>
/// <param name="contentType">The MIME type of the file to upload</param>
/// <returns>This request</returns>
IRestRequest AddFile(string name, string path, string contentType);

/// <summary>
/// Adds the bytes to the Files collection with the specified file name
/// </summary>
Expand Down
29 changes: 29 additions & 0 deletions RestSharp/RestRequest.cs
Original file line number Diff line number Diff line change
Expand Up @@ -141,6 +141,35 @@ public IRestRequest AddFile(string name, string path)
});
}

/// <summary>
/// Adds a file to the Files collection to be included with a POST or PUT request with the specified content type
/// (other methods do not support file uploads).
/// </summary>
/// <param name="name">The parameter name to use in the request</param>
/// <param name="path">Full path to file to upload</param>
/// <param name="contentType">The MIME type of the file to upload</param>
/// <returns>This request</returns>
public IRestRequest AddFile(string name, string path, string contentType)
{
FileInfo f = new FileInfo(path);
long fileLength = f.Length;

return AddFile(new FileParameter
{
Name = name,
FileName = Path.GetFileName(path),
ContentLength = fileLength,
Writer = s =>
{
using (var file = new StreamReader(path))
{
file.BaseStream.CopyTo(s);
}
},
ContentType = contentType
});
}

/// <summary>
/// Adds the bytes to the Files collection with the specified file name
/// </summary>
Expand Down