3

I Want to upload document to the Document library's specific folder using JavaScript/JQuery. I tried with the below code. But I can upload documents to the root folder only not to the specific folder.

function sendFileToServer(library, filename, file) 
{
    var reader = new FileReader();
    reader.onloadend = function(evt) 
    {
      if (evt.target.readyState == FileReader.DONE) 
      {
        var serverRelativeUrlToFolder = '/AttachmentLib/Test';
         var buffer = evt.target.result;
         //Upload Files to the Root Folder
         //var completeUrl = String.format(
            //"//{0}/_api/Web/Lists/getByTitle('AttachmentLib')/RootFolder/Files/Add(url='{1//}', //overwrite=true)",
            //_spPageContextInfo.webAbsoluteUrl, filename);
            //Upload Files to the Specific folder
        var completeUrl = String.format(
                 "{0}/_api/web/getfolderbyserverrelativeurl('{1}')/files" +
                 "/add(overwrite=true, url='{2}')",
                 _spPageContextInfo.webAbsoluteUrl, serverRelativeUrlToFolder, filename);
          $.ajax({
                url: completeUrl,
                type: "POST",
                data: buffer,
                async: false,
                processData: false,
                headers: {
                    "accept": "application/json;odata=verbose",
                    "X-RequestDigest": $("#__REQUESTDIGEST").val()
                    //"content-length": buffer.byteLength
                },
                complete: function (data) {
                    alert('succes');
                },
                error: function (err) {
                     alert('failed');
                }
        });
      }
    };
    reader.readAsArrayBuffer(file);
}
1

Solved!!!
I done this with the below code

function sendFileToServer(filename, file) 
{
    var reader = new FileReader();
    reader.onloadend = function(evt) 
    {
        if (evt.target.readyState == FileReader.DONE) 
        {
            var folder = localStorage.getItem('FolderName');

            var serverRelativeUrlToFolder = 'LibraryName/' + folder;
            var buffer = evt.target.result;
            //Upload to Specific folder 
            var docLibraryEndpoint = String.format( "{0}/_api/web/getfolderbyserverrelativeurl('{1}')/files" +
                            "/add(overwrite=true,url='{2}')",
                            _spPageContextInfo.webAbsoluteUrl, serverRelativeUrlToFolder, filename
                            );
            $.ajax({
                url: docLibraryEndpoint,
                type: "POST",
                data: buffer,
                processData: false,
                headers: {
                    "accept": "application/json;odata=verbose",
                    "X-RequestDigest": $("#__REQUESTDIGEST").val()
                },
                success:function()
                {
                    alert("Document uploaded successfully");
                },
                error:function()
                {
                    alert("Failed to upload the document");
                }
            });
        }
    };
    reader.readAsArrayBuffer(file);
}

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.