Httpclient and multipartformdatacontent are at least suitable for. Net Framework version 4.5
Sender code
using (HttpClient client = new HttpClient())
{
var content = new MultipartFormDataContent();
//Add a string parameter named qq
content.Add(new StringContent("123456"), "qq");
string path = Path.Combine(System.Environment.CurrentDirectory, "1.png");
//add a file parameter named files, the file name is 123.png
content.Add(new ByteArrayContent(System.IO.File.ReadAllBytes(path)), "file", "123.png");
var requestUri = "http://192.168.1.108:56852/api/Test/SaveFile";
var result = client.PostAsync(requestUri, content).Result.Content.ReadAsStringAsync().Result;
Console.WriteLine(result);
}
Receiver code
[HttpPost]
public async Task<JsonResult> SaveFile([FromForm]string qq, IFormFile file)
{
return await Task.Run(() =>
{
try
{
//save the file
var filefullPath = Path.Combine(Directory.GetCurrentDirectory(), file.FileName);
using (FileStream fs = new FileStream(filefullPath, FileMode.Create))
{
file.CopyTo(fs);
fs.Flush();
}
}
catch (Exception ex)
{
return Fail(file.FileName + "---" + ex.Message);
}
return Success();
});
}
Note: if you want to receive data in the form of parameters, you need to make sure that the parameter name is consistent with the name set in the sending request above. Otherwise, it cannot be automatically bound to the parameter, and you need to mark the parameter with [fromform].
Using model object to receive data
public class SaveFileModel
{
public string qq { get; set; }
public IFormFile File { get; set; }
}
public async Task<JsonResult> SaveFile([FromForm]SaveFileModel model)
{
//......
}
Use httpcontext to get data from the requested form
public async Task<JsonResult> SaveFile()
{
return await Task.Run(() =>
{
var files = HttpContext.Request.Form.Files;
var qq = HttpContext.Request.Form["qq"];
//......
});
}
Summary
this problem is encountered when writing a. Net core project. In the past, in. Net framework 4.0, string was used to splice the contents of files in the form, and there were boundary lines everywhere. Multipartformdatacontent is used for form submission. The upload file of C # tutorial helps us to splice this complex content. (you can grab the request with Fiddler) EMM I’ll find out how to upload the original file, and update this article if there is one.
The above is the details of C # using httpclient to upload files with other parameters
Read More:
- How to use C # to get image format without system. Drawing. Common
- Error: ENOENT: no such file or directory, open ‘E:\Program Files\nodejs\tmp\upload_38b32f25bc984333c
- How to use C + + function pointer array
- Error: could not open `C:\Program Files\Java\jre1.8.0_211\lib\amd64\jvm.cfg’ (How to Fix)
- How to use it RegAsm.exe Register C in a development / production environment#/ VB.NET Developed DLL
- Ng Zorro antd file stream upload, access to upload file path
- How to search files or folders in Ubuntu
- How to Use Truffle to Deploy contracts on moonbeam
- Four ways to get Django parameters in request
- [Solved] IE Browser Upload Files Error: org.apache.tomcat.util.http.fileupload.FileUploadException: Stream ended unexpectedly
- [Solved] Logon failed, use ctrl+c to cancel basic credential prompt. unable to access
- How to use matlab to solve equation
- How to batch create folders in Excel form and copy the specified files to a new folder
- How to use JavaScript in HTML
- Mybatis passes in multiple parameters to mapper. And uses @param details to report an error
- class.getResource () and ClassLoader.getResource How to fill in the parameters in ()
- tf.one_ How to use hot ()
- How to delete the residual software files on the computer?
- How to Solve failed Precondition Error: attempting to use uninitialized value beta1_power
- Winscp failed to upload the file to the server, indicating permission denied with return code 3