c#MultipartFormDataContent添加方法(如何正确添加文件) [英] c# MultipartFormDataContent Add methods (how to properly add a file)
问题描述
我正在使用MultipartFormDataContent将文件上传到rest API.这工作得很好,但是我的问题集中在使用Add(...)方法包括文件内容的正确方法.目前,我正在这样做:
I'm using the MultipartFormDataContent to upload a file to a rest API. This is working well, but my questions focuses on the proper way to use the Add(...) method to include the file content. Currently I am doing such:
string fileName = "foobar.txt";
MultipartFormDataContent formContent = new MultipartFormDataContent();
ByteArrayContent byteArray = ...;
formContent.Add(byteArray, "file", fileName);
...
再次
可行,我试图理解Add(...)方法的参数.在以下位置的MSDN文档中:https://msdn.microsoft.com/zh-CN/library/system.net.http.multipartformdatacontent(v=vs.118).aspx
它有两种添加方法:
- 添加(HttpContent,字符串)
- 添加(HttpContent,String,String)
但是都没有列出描述,并且在深入研究方法本身时,参数仅(同样没有描述)描述为:
however neither has a description listed, and when drilling into the methods themselves, the parameters are only described (again without descriptions) as:
- HttpContent内容,字符串名称
- HttpContent内容,字符串名称,字符串fileName
所以,在此情况下,我的具体问题是:
- "名称"参数是什么?(我将其设置为文件"的那个)?
- 这需要是文字字符串"file"还是其他名称?
- 如何使用?
- What is the 'name' parameter? (the one I'm setting as "file")?
- Does this need to be the literal string "file" or can it be something else?
- How is it used?
推荐答案
它被写入内容配置标头中.您似乎可以将其保留下来以上传文件.名称看起来与输入名称相对应.
It is written into the content disposition header. it looks like you can leave it off for a file upload. name looks like it corresponds to the input name.
https://developer.mozilla.org/en-US/docs/Web/HTTP/Headers/Content-Disposition
这篇关于c#MultipartFormDataContent添加方法(如何正确添加文件)的文章就介绍到这了,希望我们推荐的答案对大家有所帮助,也希望大家多多支持IT屋!