Right now I have multiple forms on a page that I want to be rid of. So currently I'm doing this.
JAVASCRIPT
$("button[id$='uploadfile']").click(function (evt) {
//////////////////////////////////////////////////////
// This code block works
alert("upload file");
var file1 = $('#csvIdentifierFileBase').val();
var formData = new FormData();
var opmlFile = $('#csvIdentifierFileBase')[0];
formData.append("opmlFile", opmlFile.files[0]);
var xhr = new XMLHttpRequest();
xhr.open('POST', 'alt/Import');
xhr.send(formData);
xhr.onreadystatechange = function() {
if (xhr.readyState == 4 && xhr.status == 200) {
alert(xhr.responseText);
}
};
//////////////////////////////////////////////////////
//////////////////////////////////////////////////////
// This code block does not
$.ajax({
type: 'POST',
url: 'alt/Import',
data: formData,
enctype: 'multipart/form-data',
//cache: false,
//contentType: false,
//processData: false,
success: function (result) {
alert(result.success);
},
error: function (a, b, c) {
alert("An error has occurred.");
}
});
HTML
<div class="col-md-12">
<input type="file" name="csvIdentifierFileBase" id="csvIdentifierFileBase"/>
<button id="uploadfile" name="action" value="Import" >Import</button>
</div>
CONTROLLER
[System.Web.Http.HttpPost]
public ActionResult Import()//HttpPostedFileBase csvIdentifierFileBase)
{
HttpResponseMessage result = null;
HttpPostedFileBase file = Request.Files[0]; //Uploaded file
}
Not changing anything besides the javscript call to this controller method, and using the XmlHttpRequest it works fine, is there a way to switch to a jquery/ajax style call so I'm not relying on the XmlHttpRequest object and still have it call into the controller? I'm looking for a native solution and not another type of plugin... is that possible? also can't wrap the input and button in a AJAX.BeginForm tag as I'm trying to eliminate the number of forms on one page.