How to post form-data IFormFile with HttpClient?

后端 未结 4 1593
暖寄归人
暖寄归人 2021-01-04 07:22

I have backend endpoint Task Post(IFormFile csvFile) and I need to call this endpoint from HttpClient. Currently I am getting Unsuppor

4条回答
  •  情歌与酒
    2021-01-04 07:36

    Solved by using this code:

            const string fileName = "csvFile.csv";
            var filePath = Path.Combine("IntegrationTests", fileName);
            var bytes = File.ReadAllBytes(filePath);
            var form = new MultipartFormDataContent();
            var content = new StreamContent(new MemoryStream(bytes));
            form.Add(content, "csvFile");
            content.Headers.ContentDisposition = new ContentDispositionHeaderValue("form-data")
            {
                Name = "csvFile",
                FileName = fileName
            };
            content.Headers.Remove("Content-Type");
            content.Headers.Add("Content-Type", "application/octet-stream; boundary=----WebKitFormBoundaryMRxYYlVt8KWT8TU3");
            form.Add(content);
    
            //Act
            var postResponse = await _sellerClient.PostAsync("items/upload", form);
    

提交回复
热议问题