I have a jQuery script that posts a file to an ASP page using Ajax.
It works fine with Chrome, but I am experiencing problems with IE9.
Because IE9 wasn't reading the FormData() function, I added the if statement:
if(typeof FormData == "undefined"){
But now I am getting this error in the console:
SCRIPT5007: Unable to get value of the property 'length': object is null or undefined
Here is the full code:
<INPUT type="file" name="file" id="file">
<a id="upload_file" class="button">
Upload File
</a>
<script>
$("a#upload_file").click(function(){
if(typeof FormData == "undefined"){
var data = [];
jQuery.each(jQuery('#file')[0].files, function(i, file) {
data.push('file-'+i, file);
});
} else {
var data = new FormData();
jQuery.each(jQuery('#file')[0].files, function(i, file) {
data.append('file-'+i, file);
});
}
jQuery.ajax({
url: 'file_save.asp',
data: data,
cache: false,
contentType: false,
processData: false,
type: 'POST',
success: function(data){
$("#upload_success").text("Your file has uploaded successfully.");
$("#file_location").val("files/" + data);
$(".filename").html("");
}
});
});
</script>