Home > Backend Development > C++ > How to Perform HTTP POST File Uploads in C#?

How to Perform HTTP POST File Uploads in C#?

DDD
Release: 2025-01-11 19:32:44
Original
575 people have browsed it

How to Perform HTTP POST File Uploads in C#?

HTTP POST file upload in C#

Sending files via HTTP POST is a common task in web applications. In C#, you can use HttpWebRequest classes and MultipartFormDataContent objects to achieve this functionality.

Create HttpWebRequest object

First, create a HttpWebRequest object and specify the URI of the web server. Then, set the Method attribute to "POST" and the ContentType attribute to the appropriate content type of the file being sent.

<code class="language-csharp">HttpWebRequest req = WebRequest.Create(uri) as HttpWebRequest;
req.Method = "POST";
req.ContentType = file.ContentType;</code>
Copy after login

File ready for upload

To send a file, create a MultipartFormDataContent object and include it in the request. MultipartFormDataContent Allows you to send both form data and binary data in one request.

<code class="language-csharp">using (var formData = new MultipartFormDataContent())
{
    formData.Add(new StringContent(file.Name), "file");
    formData.Add(new StreamContent(file.Content), file.Name, file.Name);
}</code>
Copy after login

Send request

Finally, use GetResponse() or GetResponseAsync() to send the request to the web server.

<code class="language-csharp">HttpWebResponse response = null;

try
{
    response = req.GetResponse() as HttpWebResponse;
}
catch (Exception e)
{
    // 处理异常
}</code>
Copy after login

Alternatives for .NET 4.0 and below

For .NET 4.0 and below, you can use the Microsoft.Net.Http package from NuGet to simplify the file upload process:

<code class="language-csharp">using System.Net.Http;
using System.Net.Http.Headers;

private async Task<System.IO.Stream> Upload(string actionUrl, string paramString, Stream paramFileStream, byte[] paramFileBytes)
{
    using (var client = new HttpClient())
    {
        using (var formData = new MultipartFormDataContent())
        {
            formData.Add(new StringContent(paramString), "param1");
            formData.Add(new StreamContent(paramFileStream), "file1");
            formData.Add(new ByteArrayContent(paramFileBytes), "file2");

            var response = await client.PostAsync(actionUrl, formData);
            if (!response.IsSuccessStatusCode)
            {
                return null;
            }
            return await response.Content.ReadAsStreamAsync();
        }
    }
}</code>
Copy after login

The above is the detailed content of How to Perform HTTP POST File Uploads in C#?. For more information, please follow other related articles on the PHP Chinese website!

source:php.cn
Statement of this Website
The content of this article is voluntarily contributed by netizens, and the copyright belongs to the original author. This site does not assume corresponding legal responsibility. If you find any content suspected of plagiarism or infringement, please contact admin@php.cn
Popular Tutorials
More>
Latest Downloads
More>
Web Effects
Website Source Code
Website Materials
Front End Template