Reputation: 14234
I am working on exposing some REST-based services via ASP.NET MVC 3. These services will be hit via JQuery as well as a Windows Phone Silverligh app. I know how to interact with a typical service. For instance, I currently have ones like the followng:
[AcceptVerbs(HttpVerbs.Post)]
public ActionResult AddComment(string username, string comment)
{
// Do stuff
return Json(new { message = "Success" });
}
I want to expose a REST-based service that allows users to upload a file. The trick here is that I also need to pass some data along with each file. However, I'm not sure how to do that. Every example I find only has just a file. But I'm not sure of
Everything else I passed is just strings. However, in this I seem to have data serialized in binary format because of the file, and some string text. Because of that, I'm not sure what to do. Am I making sense?
Upvotes: 3
Views: 538
Reputation: 1561
The signature for the action should just be: public ActionResult MyAction(string username, string comment, HttpPostedFileBase file1) { ... }
MVC binding should examine the request and match the form submission to the action based on the parameter names and types.
The clientside form must have enctype = "multipart/form-data" with method POST.
JQuery would just post the form with $("#form").submit().
Upvotes: 4