我有Web api控制器Uploads Controller,它有var request = new RestRequest("Uploads", Method.POST);
request.RequestFormat = DataFormat.Json;
request.AddHeader("Content-Type", "application/json");
request.AddFile("filename", Server.MapPath("/Images/137549014628194.R6MyHlYrIfIo3BWPIytG_height640.png"), "image/png");
request.AddFile("filename", Server.MapPath("/Images/137549014628194.R6MyHlYrIfIo3BWPIytG_height640.png"), "image/png");
request.AddFile("filename", Server.MapPath("/Images/137549014628194.R6MyHlYrIfIo3BWPIytG_height640.png"), "image/png");
request.AddParameter("participantsId", 2);
request.AddParameter("taskId", 77);
request.AddParameter("EnteredAnswerOptionId", 235);
IRestResponse response = createClient().Execute(request);
方法将数据存储到数据库。
现在我正在尝试将文件和一些参数发布到该web api但是所有尝试都失败了,就像通过数组列表,json对象一样,我们无法将文件和参数发布到web api?
[HttpPost]
public string PostUpload(int? participantsId, int? taskId, int? EnteredAnswerOptionId)
{
var file = HttpContext.Current.Request.Files.Count > 0 ?
HttpContext.Current.Request.Files[0] : null;
if (file.ContentLength > 0)
{
var fileName = Path.GetFileName(file.FileName);
var path = Path.Combine(HttpContext.Current.Server.MapPath("~/uploads"), fileName);
file.SaveAs(path);
}
return "/uploads/" + file.FileName;
}
web api方法:
Application Pool Identity
但它会给出错误:
ExceptionMessage“:”没有MediaTypeFormatter可用于从媒体类型的内容中读取“xxxx”类型的对象 “多部分/格式数据
我需要将文件和参数发布到我的api。
使用 restsharp
发送数据