Dear All,
Today I am going to tell you how to upload a file from jquery ajax. First of all you need is a jquery reference and a .cs file as I am doing it in ASP.net.
Step 1 : Create a form, Here I have created a form and included a file upload html <input type="file" id="fileUpload"> and simple button with name "btnUploadFile".
Step 2: Write the javascript code to upload the file
<script type="text/javascript">
$(document).ready(function () {
$('#btnUploadFile').on('click', function () {
var files = $("#fileUpload").get(0).files;
var data = new FormData();
data.append('data', files[0]);
// Make Ajax request with the contentType = false, and processData = false
var ajaxRequest = $.ajax({
type: "POST",
url: "SendFile.aspx/uploadfile",
contentType: false,
processData: false,
data: data,
success: function (data) {
alert('File uploaded Successfully');
},
error: function (xhr, ajaxoptions, thrownerror) {
alert(xhr.status);
alert(thrownerror);
},
asyn:false
//data: data
});
ajaxRequest.done(function (xhr, textStatus) {
// Do other operation
});
});
});
</script>
Today I am going to tell you how to upload a file from jquery ajax. First of all you need is a jquery reference and a .cs file as I am doing it in ASP.net.
Step 1 : Create a form, Here I have created a form and included a file upload html <input type="file" id="fileUpload"> and simple button with name "btnUploadFile".
Step 2: Write the javascript code to upload the file
<script type="text/javascript">
$(document).ready(function () {
$('#btnUploadFile').on('click', function () {
var files = $("#fileUpload").get(0).files;
var data = new FormData();
data.append('data', files[0]);
// Make Ajax request with the contentType = false, and processData = false
var ajaxRequest = $.ajax({
type: "POST",
url: "SendFile.aspx/uploadfile",
contentType: false,
processData: false,
data: data,
success: function (data) {
alert('File uploaded Successfully');
},
error: function (xhr, ajaxoptions, thrownerror) {
alert(xhr.status);
alert(thrownerror);
},
asyn:false
//data: data
});
ajaxRequest.done(function (xhr, textStatus) {
// Do other operation
});
});
});
</script>