Skip to content
New issue

Have a question about this project? Sign up for a free GitHub account to open an issue and contact its maintainers and the community.

By clicking “Sign up for GitHub”, you agree to our terms of service and privacy statement. We’ll occasionally send you account related emails.

Already on GitHub? Sign in to your account

ASP.​NET Core on .NET 7.0 - File upload and streams using Minimal API #172

Open
utterances-bot opened this issue Nov 18, 2022 · 2 comments

Comments

@utterances-bot
Copy link

ASP.​NET Core on .NET 7.0 - File upload and streams using Minimal API

https://asp.net-hacker.rocks/2022/04/01/aspnetcore7-files-and-streams-in-minimalapi.html

Copy link

Given these endpoints you've written, do you know the proper way to call them using HttpClient? I've tried lots of variations, but I keep getting issues such as status code 415, header problems, streams that errantly include the multipart boundary instead of just the file, etc.

Copy link

@ronwarner this is the IFormFileCollection upload code:

    private async Task UploadAsync(string url)
    {
        using HttpClient httpClient = new();
        MultipartFormDataContent content = new();

        using var stream1 = new MemoryStream("Content1"u8.ToArray());
        var fileContent1 = new StreamContent(stream1);
        fileContent1.Headers.ContentType = MediaTypeHeaderValue.Parse("application/octet-stream");
        content.Add(fileContent1, "files", "File1.txt");

        using var stream2 = new MemoryStream("Content2"u8.ToArray());
        var fileContent2 = new StreamContent(stream2);
        fileContent1.Headers.ContentType = MediaTypeHeaderValue.Parse("application/octet-stream");
        content.Add(fileContent2, "files", "File2.txt");

        var response = await httpClient.PostAsync(url, content);
        response.EnsureSuccessStatusCode();

        string responseContent = await response.Content.ReadAsStringAsync();

        Console.WriteLine(responseContent);
    }

Sign up for free to join this conversation on GitHub. Already have an account? Sign in to comment
Labels
None yet
Projects
None yet
Development

No branches or pull requests

3 participants