C#发布上传文件,如curl.exe" -F

时间:2015-07-02 18:55:56

标签: c# curl upload dotnet-httpclient

我试图在C#中复制以下curl功能,但无济于事。我收到了一个带有C#代码的错误请求。 .NET代码需要复制curl文件附件/上传功能。我很感激帮助。

" curl.exe" -F" attachment = @ c:\ data \ img.jpg" " example.com"

    public static async void Post(string fileName)
    {
        string url = "example.com";
        var stream = new FileStream(fileName, FileMode.Open);
        string name = Path.GetFileName(fileName);

        var client = new HttpClient { Timeout = TimeSpan.FromMinutes(10) };

        var streamContent = new StreamContent(stream);
        streamContent.Headers.ContentDisposition = new ContentDispositionHeaderValue("form-data");
        streamContent.Headers.ContentDisposition.Name = "\"file\"";
        streamContent.Headers.ContentDisposition.FileName = "\"" + name + "\"";
        streamContent.Headers.ContentType = new MediaTypeHeaderValue("application/octet-stream");

        var content = new MultipartFormDataContent { streamContent };
        HttpResponseMessage message = await client.PostAsync(url, content);
        string s = await message.Content.ReadAsStringAsync();
    }

1 个答案:

答案 0 :(得分:-1)

解决了我的问题。 "附件"需要成为文件。