From 017153bf1cafc7730f3bbd34aa0ae20f76f9049c Mon Sep 17 00:00:00 2001 From: Todd Menier Date: Sun, 20 Jan 2019 10:18:13 -0600 Subject: [PATCH] #402 multipart enhancement allow specifying different filename when uploading from local path --- Test/Flurl.Test/Http/MultipartTests.cs | 27 +++++++++++-------- .../Content/CapturedMultipartContent.cs | 5 ++-- 2 files changed, 19 insertions(+), 13 deletions(-) diff --git a/Test/Flurl.Test/Http/MultipartTests.cs b/Test/Flurl.Test/Http/MultipartTests.cs index 4d5e82f..91419f8 100644 --- a/Test/Flurl.Test/Http/MultipartTests.cs +++ b/Test/Flurl.Test/Http/MultipartTests.cs @@ -18,31 +18,36 @@ namespace Flurl.Test.Http var content = new CapturedMultipartContent() .AddString("string", "foo") .AddStringParts(new { part1 = 1, part2 = 2, part3 = (string)null }) // part3 should be excluded - .AddFile("file", Path.Combine("path", "to", "image.jpg"), "image/jpeg") + .AddFile("file1", Path.Combine("path", "to", "image1.jpg"), "image/jpeg") + .AddFile("file2", Path.Combine("path", "to", "image2.jpg"), "image/jpeg", fileName: "new-name.jpg") .AddJson("json", new { foo = "bar" }) .AddUrlEncoded("urlEnc", new { fizz = "buzz" }); - Assert.AreEqual(6, content.Parts.Length); + Assert.AreEqual(7, content.Parts.Length); AssertStringPart(content.Parts[0], "string", "foo"); AssertStringPart(content.Parts[1], "part1", "1"); AssertStringPart(content.Parts[2], "part2", "2"); - AssertStringPart(content.Parts[4], "json", "{\"foo\":\"bar\"}"); - AssertStringPart(content.Parts[5], "urlEnc", "fizz=buzz"); - - Assert.AreEqual("file", content.Parts[3].Headers.ContentDisposition.Name); - Assert.AreEqual("image.jpg", content.Parts[3].Headers.ContentDisposition.FileName); - Assert.IsInstanceOf(content.Parts[3]); - Assert.AreEqual("image/jpeg", content.Parts[3].Headers.ContentType?.MediaType); + AssertFilePart(content.Parts[3], "file1", "image1.jpg", "image/jpeg"); + AssertFilePart(content.Parts[4], "file2", "new-name.jpg", "image/jpeg"); + AssertStringPart(content.Parts[5], "json", "{\"foo\":\"bar\"}"); + AssertStringPart(content.Parts[6], "urlEnc", "fizz=buzz"); } - private void AssertStringPart(HttpContent part, string name, string content) { + private void AssertStringPart(HttpContent part, string name, string content) { + Assert.IsInstanceOf(part); Assert.AreEqual(name, part.Headers.ContentDisposition.Name); Assert.AreEqual(content, (part as CapturedStringContent)?.Content); - Assert.IsInstanceOf(part); Assert.IsFalse(part.Headers.Contains("Content-Type")); // #392 } + private void AssertFilePart(HttpContent part, string name, string fileName, string contentType) { + Assert.IsInstanceOf(part); + Assert.AreEqual(name, part.Headers.ContentDisposition.Name); + Assert.AreEqual(fileName, part.Headers.ContentDisposition.FileName); + Assert.AreEqual(contentType, part.Headers.ContentType?.MediaType); + } + [Test] public void must_provide_required_args_to_builder() { var content = new CapturedMultipartContent(); diff --git a/src/Flurl.Http/Content/CapturedMultipartContent.cs b/src/Flurl.Http/Content/CapturedMultipartContent.cs index e19ca6c..7cd4ca5 100644 --- a/src/Flurl.Http/Content/CapturedMultipartContent.cs +++ b/src/Flurl.Http/Content/CapturedMultipartContent.cs @@ -105,9 +105,10 @@ namespace Flurl.Http.Content /// The local path to the file. /// The media type of the file. /// The buffer size of the stream upload in bytes. Defaults to 4096. + /// The filename, added to the Content-Disposition header of the part. Defaults to local file name. /// This CapturedMultipartContent instance (supports method chaining). - public CapturedMultipartContent AddFile(string name, string path, string mediaType = null, int bufferSize = 4096) { - var fileName = FileUtil.GetFileName(path); + public CapturedMultipartContent AddFile(string name, string path, string mediaType = null, int bufferSize = 4096, string fileName = null) { + fileName = fileName ?? FileUtil.GetFileName(path); var content = new FileContent(path, bufferSize); if (mediaType != null) content.Headers.ContentType = new MediaTypeHeaderValue(mediaType);