2

We are having options to upload attachments/files in to document library or to list as attachment with the help of JSOM by using FILEREADER object.

FILEREADER is available only after IE10 versions.

I have a requirement to implement this functionality in IE08 version without using FILEREADER, can you please suggest any way to do this?

0

If you want to target only IE then you can use Microsoft ActiveXObject. Since ActiveXObject are part, they wont be available for other browsers. You can check for the browser first and then version, if the version is less then 10, then create an object like below:

var fileReader = ActiveXObject("Scripting.FileSystemObject");
var file = fileReader.OpenTextFile(file_path, 1, true);
var fileContent = file.ReadAll();
file.Close();
0

Upload attachment is quite simple, first you need to read the file content and get the buffer of it.

function getFileBuffer(v) {
    var deferred = jQuery.Deferred();
    var reader = new FileReader();
    reader.onloadend = function (e) {
        deferred.resolve(e.target.result);
    }
    reader.onerror = function (e) {
        deferred.reject(e.target.error);
    }
    reader.readAsArrayBuffer(v);
    return deferred.promise();
}

This code do that job. Once you get the buffer you need to change it to Base64String

getFileBuffer(file).then(
    function (buffer) {
        var bytes = new Uint8Array(buffer);
        var binary = '';
        for (var b = 0; b < bytes.length; b++) {
            binary += String.fromCharCode(bytes[b]);
        }
        //we will get here soon
        //the code 1
    }
)

After that you can call Full Rest api to upload the attachment, I always execute the code with a RequestExecutor, like this:

//code 1
var scriptbase = _spPageContextInfo.webServerRelativeUrl + "/_layouts/15/";
$.getScript(scriptbase + "SP.RequestExecutor.js", function () {
    var createitem = new SP.RequestExecutor(_spPageContextInfo.webServerRelativeUrl);
    createitem.executeAsync({
        url: _spPageContextInfo.webServerRelativeUrl + "/_api/web/lists/GetByTitle('" + listName + "')/items(" + id + ")/AttachmentFiles/add(FileName='" + file.name + "')",
        method: "POST",
        binaryStringRequestBody: true,
        body: binary, //<-- this is your base64 String :O
        success: fsucc,
        error: ferr,
        state: "Update"
    });
    function fsucc(data) {
        //ok function :)
    }
    function ferr(data) {        
        //fail function :(
    }
});

Your Answer

By clicking “Post Your Answer”, you agree to our terms of service, privacy policy and cookie policy

Not the answer you're looking for? Browse other questions tagged or ask your own question.