Created
July 19, 2018 06:56
-
-
Save hudsonmendes/f4466ef844331c0843a59d1cd2f086c8 to your computer and use it in GitHub Desktop.
Test Utility class for creating POST MultipartFormData sending files (like a browser would) to the server
This file contains hidden or bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
using System.Drawing; | |
using System.Net.Http; | |
using System.Net.Http.Headers; | |
namespace Infra.Assets.TestUtils | |
{ | |
public static class TestHttpContentUtils | |
{ | |
const string FileFormFieldName = "file"; | |
public static HttpContent FileContent( | |
string fileName, | |
string contentType, | |
Image image) | |
{ | |
var content = new MultipartFormDataContent(); | |
var stream = TestImageUtils.StreamFrom(image); | |
var disposition = new ContentDispositionHeaderValue("form-data"); | |
disposition.Name = FileFormFieldName; | |
disposition.FileName = fileName; | |
var file = new StreamContent(stream); | |
file.Headers.ContentLength = stream.Length; | |
file.Headers.ContentDisposition = disposition; | |
file.Headers.ContentType = new MediaTypeHeaderValue(contentType); | |
content.Add(file, FileFormFieldName); | |
return content; | |
} | |
} | |
} |
Sign up for free
to join this conversation on GitHub.
Already have an account?
Sign in to comment