简体   繁体   中英

Uploading Files with ASP.NET MVC 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?

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

The technical post webpages of this site follow the CC BY-SA 4.0 protocol. If you need to reprint, please indicate the site URL or the original address.Any question please contact:yoyou2525@163.com.

 
粤ICP备18138465号  © 2020-2024 STACKOOM.COM