C # - What type of FormData object
I have a small form that will allow users to upload a single file. The shape is as shown below.
<form action="/Interviews/Create" enctype="multipart/form-data" id="createInterviewForm" method="post">
<div class="form-group">
<div class="col-xs-6">
<label>File Upload:</label>
<input type="file" id="txtInterviewUploadFile">
</div>
</div>
</div>
<div class="form-group">
<div class="col-md-10">
<input type="submit" value="Create" class="btn btn-primary" />
<input type="submit" value="Cancelar" class="btn btn-primary" id="btnCancelInterviewCreation" />
</div>
</div>
I am uploading a file via ajax, so I have the following code
$("form#createInterviewForm").submit(function (e) {
e.preventDefault();
var fd = new FormData($(this)[0]);
$.ajax({
url: '/Interviews/AjaxCreate',
data: fd,
processData: false,
contentType: false,
type: 'POST',
success: function (data) {
alert(data);
}
});
});
On my controller, I have something like this
[HttpPost]
public JsonResult AjaxCreate(??What datatype?? postedData)
{
return Json(true);
}
Question When I post a message, the message reaches my controller. My problem here is that I cannot figure out what the type of the FormData object is. Can anyone help me?
+3
source to share
2 answers
So I finally got this to work. This is what I did:
In ajax, I have the following:
$("form#createInterviewForm").submit(function (e) {
e.preventDefault();
var formData = new FormData($(this)[0]);
formData.append('file', $("#txtInterviewUploadFile")[0].files[0]);
$.ajax({
url: "/Interviews/AjaxCreate",
type: 'POST',
data: formData,
contentType: false,
processData: false,
success: function (data) {
alert(data)
}
});
return false;
});
This will post all input and file input.
On the controller, all I had to do was use Request.Form ["Key"] and Request.Files to access all the information
+1
source to share
Departure HttpPostedFileBase
. Link
(From MSDN) Serves as the base class for classes that provide access to individual
files that have been uploaded by a client.
Your controller action will be:
[HttpPost]
public JsonResult AjaxCreate(HttpPostedFileBase postedData)
{
return Json(true);
}
0
source to share