I have Web api controller Uploads Controller which have PostUpload
method to store data to database.
Now I'm trying to post file and some parameter to that web api but all try are fail like pass by array list ,json object ,we can't post file and parameter to web api?
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 method:
[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;
}
but it give error like:
ExceptionMessage":"No MediaTypeFormatter is available to read an object of type 'xxxx' from content with media type 'multipart/form-data
I need to post file as well as parameter to my api.
sending data using restsharp