Call A Multi-Part Form Method Programmatically

后端 未结 1 2007
Happy的楠姐
Happy的楠姐 2021-01-24 22:41

I have the following method in my WebApi

[HttpPost]
[Route(\"foo/bar\")]
[Consumes(\"multipart/form-data\")]
[DisableRequestSizeLimit]
public async Task

        
相关标签:
1条回答
  • 2021-01-24 23:31

    Try to use HttpClient and send MultipartFormDataContent in controller

    using (var client = new HttpClient())
    {
        using (var content = new MultipartFormDataContent())
        {
            content.Add(new StringContent("testA"), "A");//string
            content.Add(new StringContent("testB"), "B");
            content.Add(new StringContent("testBB"), "B");//string[]
            content.Add(new StringContent("testC"), "C");
            content.Add(new StringContent("testCC"), "C");
            
            //replace with your own file path, below use an image in wwwroot for example
            string filePath = Path.Combine(_hostingEnvironment.WebRootPath + "\\Images", "myImage.PNG");
    
            byte[] file = System.IO.File.ReadAllBytes(filePath);
                    
            var byteArrayContent = new ByteArrayContent(file);
    
            content.Add(byteArrayContent, "file", "myfile.PNG");
            
            var url = "https://locahhost:5001/foo/bar";
            var result = await client.PostAsync(url, content);
        }
    }
    
    0 讨论(0)
提交回复
热议问题