Kashif Nizam Qureshi

Upload Document From PDP to Project Site

Leave a comment


<input id="inputFile" type="file"/><input id="uploadDocumentButton" onclick="uploadFile()" type="Button" value="Upload Document"/><script src="/PWA/SiteAssets/jquery.min.js"></script><script type="text/javascript">
//Function attached to the html fileupload tag (<input type="file">)
function uploadFile(){
    if (document.getElementById("inputFile").files.length === 0) {
        alert('No file was selected');
        return;
    }
    var parts = document.getElementById("inputFile").value.split("\\");
    var ExtPart = document.getElementById("inputFile").value.split('.').pop();

//Limiting project name to 32 characters long,
//as this will increase the file upload ajax call.
        
    var filename = "NYUAD-PMO_" + PDP_projName.substring(0,32) + "_Project Charter-Template_v1_0." + ExtPart;

    //var filename = "NYUAD-PMO_" + PDP_projName + "_Project Charter-Template_v1_0." + ExtPart;
    var file = document.getElementById("inputFile").files[0];
    var getUrl = "http://sps2013/PWA/" + PDP_projName;
  //If project name contains spaces, below method will replace it with %20 or any other special characters
    var getUrl = "http://dcwap-v225-pmo/pwa/" + encodeURIComponent(PDP_projName);
  
    
uploadFileSync(getUrl , "Documents", filename, file);
}

//Upload file synchronously
function uploadFileSync(spWebUrl , library, filename, file)
{
        var reader = new FileReader();
    reader.onloadend = function(evt)
    {
      if (evt.target.readyState == FileReader.DONE)
      {
         var buffer = evt.target.result;
         // To upload files to root folder
         //var completeUrl = spWebUrl
         //  + "/_api/web/lists/getByTitle('"+  library + "')"
         //  + "/RootFolder/Files/add(url='"+ filename +"',overwrite='true')?"
         //  + "@TargetLibrary='"+ library +"'&@TargetFileName='"+ filename +"'";

         // To upload files to sub-folder
         var completeUrl = spWebUrl
           + "/_api/web/lists/getByTitle('"+  library + "')"
           + "/RootFolder/Folders('Initiation%20Phase')/Folders('PCID')/Files/add(url='"+ filename+"',overwrite='true')?"
           + "@TargetLibrary='"+ library +"'&@TargetFileName='"+ 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);
}
</script>
Advertisements

Leave a Reply

Fill in your details below or click an icon to log in:

WordPress.com Logo

You are commenting using your WordPress.com account. Log Out / Change )

Twitter picture

You are commenting using your Twitter account. Log Out / Change )

Facebook photo

You are commenting using your Facebook account. Log Out / Change )

Google+ photo

You are commenting using your Google+ account. Log Out / Change )

Connecting to %s