3

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

  1. How to accept additional data
  2. What to pass from JQuery.

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?

3
  • 1
    Just to be clear, the sample code you posted is not RESTfull. Commented Jun 19, 2012 at 16:58
  • 1
    possible duplicate of jQuery ajax upload file in asp.net mvc Commented Jun 19, 2012 at 16:59
  • I reviewed the "jQuery ajax upload file in asp.net mvc". In my opinion, this question is not a duplicate of that. Commented Jun 19, 2012 at 17:08

1 Answer 1

4

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().

Sign up to request clarification or add additional context in comments.

2 Comments

What if I need to POST the image via the an $.ajax call?
@user70192, you cannot upload files using AJAX as explained in the duplicate post. So if you have to do this you could use a client side upload plugin. Or use the HTML5 File API.

Your Answer

By clicking “Post Your Answer”, you agree to our terms of service and acknowledge you have read our privacy policy.

Start asking to get answers

Find the answer to your question by asking.

Ask question

Explore related questions

See similar questions with these tags.