IE 8 - .files attribute workaround

190 views Asked by At

I've been trying to figure out how to attach a document to a SharePoint list after creating an item. I was able to find the code online to do this; however, after testing the code in different browsers, it doesn't work for IE 8 and IE 9 because of the .files attribute. Is there any way to go around this?

function addAttachments(itemId)
{
    var filereader = {}, file= {}; 
/* Here --------> */var files = document.getElementById('myfile').files; 
    for(var j=0; j< files.length; j++)
    {
        file = files[j]; 
        filereader = new FileReader(); 
        filereader.filename = file.name; 
        filereader.onload = function(){
            var data = this.result
            n = data.indexOf(";base64,") + 8; 

            data = data.substring(n); 

            $().SPServices({
                operation: "AddAttachment", 
                listName:'Test', 
                asynch: false, 
                listItemID: itemId, 
                fileName: this.filename, 
                attachment: data, 
                completefunc: function (xData, Status){
                }

            }); 
        }; 

        filereader.onabort = function (){
        }; 

        filereader.onerror = function(){
        }; 

        filereader.readAsDataURL(file); 
    }//end of for loop
}

Thank you in advance for your help!

1

There are 1 answers

1
sczizzo On

Yes, you can use a polyfill, which essentially implements the missing functionality in unsupported browsers. There are a few polyfills for the File API listed on Modernizr's HTML5 Cross Browser Polyfills page.