我的移动供应商正在向我发送上传文件的请求。
var client = new RestClient(URL);
var request = new RestRequest(Method.POST);
request.AddHeader("postman-token", "52606652- f26a-e809-0de1-97a6030097eb");
request.AddHeader("cache-control", "no-cache");
request.AddHeader("content-type", "multipart/form-data; boundary=---011000010111000001101001");
request.AddParameter("multipart/form-data; boundary=---011000010111000001101001", "-----011000010111000001101001\r\nContent- Disposition: form-data; name``=\"attachement\"; filename=\"[object Object]\"\r\nContent-Type: false\r\n\r\n\r\n-----011000010111000001101001--", ParameterType.RequestBody);
IRestResponse response = client.Execute(request);
请有人告诉我如何编写代码以在其他API c#上传文件。
答案 0 :(得分:0)
您可以从httpcontext中读取多部分发布的数据,如下所示:
[HttpPost]
public async Task<HttpResponseMessage> UploadDoc()
{
try
{
string root = HttpContext.Current.Server.MapPath("~/App_Data");
var provider = new MultipartFormDataStreamProvider(root);
if (System.Web.HttpContext.Current.Request.Files.Count > 0)
{
foreach (string file in System.Web.HttpContext.Current.Request.Files)
{
byte[] postedFile = null;
using (var binaryReader = new BinaryReader(System.Web.HttpContext.Current.Request.Files[file].InputStream))
{
postedFile = binaryReader.ReadBytes(System.Web.HttpContext.Current.Request.Files[file].ContentLength);
}
string fileName = System.Web.HttpContext.Current.Request.Files.AllKeys[0];
}
}
await Request.Content.ReadAsMultipartAsync(provider);