如何在这种情况下将<input type="file" id="uploadStudent" />
上传的文件发送到mvc控制器?
的Javascript
$.ajax({
url: "/Home/CompleteAttendeeType",
contentType: "application/json;charset=utf-8",
dataType: "JSON",
type: "POST",
data: JSON.stringify({
//PostedFile: "I AM CONFUSED HOW TO UPLOAD?",
AttendeeType: 1,
LicenseNumber: $("#txtLicenseNumber").val(),
LicenseState: $("#txtLicenseState").val(),
SchoolName: $("#txtSchoolName").val(),
SchoolLocation: $("#txtSchoolLocation").val()
})
})
MVC
[HttpPost]
public JsonResult CompleteAttendeeType(CompleteAttendeeTypeRequest request)
{
return Json(string.Empty, JsonRequestBehavior.AllowGet);
}
public enum AttendeeType
{
Professional,
Student,
Owner,
Guest
}
public class CompleteAttendeeTypeRequest
{
public HttpPostedFile PostedFile { get; set; }
public AttendeeType AttendeeType { get; set; }
public string LicenseNumber { get; set; }
public string LicenseState { get; set; }
public string SchoolName { get; set; }
public string SchoolLocation { get; set; }
}
答案 0 :(得分:0)
首先使用File API to read the contents of the file。
然后,您需要以可以用JSON表示的格式表达数据。如果数据是字符串,那么您可以使用它。否则,您需要转换它。 Converting it to base64是一种方法。
然后,您将在变量中包含数据,并将其包含在传递给JSON.stringify
的对象中。
如果您将其编码为base64,则需要decode it on the server。