I use JSOM to upload document in library. I use this JS:

var file;
var fileCreateInfo;
function CreateFile()
{
    // Ensure the HTML5 FileReader API is supported
    if (window.FileReader)
    {
        input = document.getElementById("fileinput");
        if (input)
        {
            file = input.files[0];
            fr = new FileReader();
            fr.onload = receivedBinary;
            fr.readAsDataURL(file);
        }
    }
    else
    {
        alert("The HTML5 FileSystem APIs are not fully supported in this browser.");
    }
}

// Callback function for onload event of FileReader
function receivedBinary()
{

    var clientContext = new SP.ClientContext('http://pk-sp:88//');
    this.oWebsite = clientContext.get_web();
    clientContext.load(this.oWebsite); 
    var list = this.oWebsite.get_lists().getByTitle("Documents"); 

    fileCreateInfo = new SP.FileCreationInformation();
    fileCreateInfo.set_url(file.name);
    fileCreateInfo.set_overwrite(true);
    fileCreateInfo.set_content(new SP.Base64EncodedByteArray());

    // Read the binary contents of the base 64 data URL into a Uint8Array
    // Append the contents of this array to the SP.FileCreationInformation
    var arr = convertDataURIToBinary(this.result);
    for (var i = 0; i < arr.length; ++i)
    {
        fileCreateInfo.get_content().append(arr[i]);
    }

    // Upload the file to the root folder of the document library
    this.newFile = list.get_rootFolder().get_files().add(fileCreateInfo);

    clientContext.load(this.newFile);
    clientContext.executeQueryAsync(onSuccess, onFailure);
}

function onSuccess()
{
    // File successfully uploaded
    alert("Success!");
}

function onFailure()
{
    // Error occurred
    alert("Request failed: " + arguments[1].get_message());
    console.log("Request failed: " + arguments[1].get_message());
}

// Utility function to remove base64 URL prefix and store base64-encoded string in a Uint8Array
// Courtesy: https://gist.github.com/borismus/1032746
function convertDataURIToBinary(dataURI)
{
    var BASE64_MARKER = ';base64,';
    var base64Index = dataURI.indexOf(BASE64_MARKER) + BASE64_MARKER.length;
    var base64 = dataURI.substring(base64Index);
    var raw = window.atob(base64);
    var rawLength = raw.length;
    var array = new Uint8Array(new ArrayBuffer(rawLength));

    for (i = 0; i < rawLength; i++)
    {
        array[i] = raw.charCodeAt(i);
    }
    return array;
}

How can i get Id of uploaded file?

I can use SPServices library to get last id or find by Title. But I think that the best way to get this ID from the fileCreateInfo object. Is that possible?

Thanks!

up vote 5 down vote accepted

You can get the ID from newFile, check out SP.File.listItemAllFields

You'll need to include the property in your load:

clientContext.load(newFile, 'ListItemAllFields');

Then you can access the ID with:

newFile.get_listItemAllFields().get_id();
  • Yes! Thanks! It's work! – Alexander Ulmaskulov Apr 7 '14 at 13:11
  • 1
    Hello, this helpd me. But i was looking for a way to update the file, when it is uploading. Thank u @Anders Aune – Bruno Gomes Feb 6 '15 at 16:55

Your Answer

 
discard

By clicking "Post Your Answer", you acknowledge that you have read our updated terms of service, privacy policy and cookie policy, and that your continued use of the website is subject to these policies.

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