Skip to content
This repository has been archived by the owner on Jan 18, 2021. It is now read-only.

File uploading api #18

Closed
rachidio opened this issue Mar 12, 2018 · 6 comments
Closed

File uploading api #18

rachidio opened this issue Mar 12, 2018 · 6 comments
Assignees
Milestone

Comments

@rachidio
Copy link

Hi,
I want upload files to a Sharepoint library, but didnt found a method to do this, do you plan to add it in the soonly?

Thanks

@AmitKB
Copy link

AmitKB commented Mar 18, 2018

Hi, I am also looking for file upload functionality. It would be really great to have that.

@gitbrent gitbrent self-assigned this Mar 19, 2018
@gitbrent
Copy link
Owner

Hi @ra6hi9 @AmitKB,

Uploading files is actually pretty easy.

Given an HTML file picker (<input type="file" id="filePicker">):
screen shot 2018-03-18 at 23 38 17

The code would be:

var reader = new FileReader();
reader.readAsArrayBuffer( $('#filePicker')[0].files[0] );
reader.onloadend = function(e){
    var parts = $('#filePicker')[0].value.split('\\');
    var fileName = parts[parts.length - 1];
    var strAjaxUrl = _spPageContextInfo.siteAbsoluteUrl
        + "/_api/web/lists/getByTitle('Site Assets')"
        + "/RootFolder/files/add(overwrite=true,url='"+ fileName +"')";

    sprLib.rest({
        url: strAjaxUrl,
        type: "POST",
        data: e.target.result
    })
    .then(function(arr){
        $('#console').append('SUCCESS: "'+ arr[0].Name +'" uploaded to: '+ arr[0].ServerRelativeUrl +'<br>');
    })
    .catch(function(strErr){
        console.error(strErr);
    });
};
reader.onerror = function(e){
    alert(e.target.error.responseText);
    console.error(e.target.error);
};

See examples/sprestlib-demo-file-upload for a working demo.

@AmitKB
Copy link

AmitKB commented Mar 19, 2018

Thanks (y) @gitbrent

@hwang74
Copy link

hwang74 commented Dec 4, 2018

please wrap it in the method since it's quite easy

@gitbrent
Copy link
Owner

Hi @ra6hi9 @AmitKB @hwang74

Thanks for your feedback.

The current codebase (future 1.9.0) has a new method: folder().upload()

It accepts an arraybuffer from either Node or browser - client browser example below using a standard HTML filepicker (<input id="filePicker" type="file">)

// STEP 1: Use FilePicker to read file
var reader = new FileReader();
reader.readAsArrayBuffer( $('#filePicker')[0].files[0] );
reader.onloadend = function(e){
    var parts = $('#filePicker')[0].value.split("\\");
    var fileName = parts[parts.length - 1];
    var foldName = $('#selDestLib').val();

    // STEP 2: Upload file to SharePoint
    sprLib.folder( foldName ).upload({
        name: fileName,
        data: e.target.result,
        overwrite: true
    });
});

Also, see examples/sprestlib-demo-file-upload.html for a live demo using a Webpart:

howto-sprestlib-upload file

@Sreehari14
Copy link

By trying above example, I am getting sprLib.folder is not a function error, can you please guide me ?

Sign up for free to subscribe to this conversation on GitHub. Already have an account? Sign in.
Projects
None yet
Development

No branches or pull requests

5 participants