1

I have a requirement to allow users to upload files > 1.5, so am using REST as opposed to CSOM. I have a JS method (shown below) that allows upload of a file from a 'file' input control on a HTML5 page.(Note I am using IE11). This works fine if the file is upto 1.5MB - over this the upload is not carried out and even the failure event is not triggered. To simplify matters I am attempting to attach the file to a list item that already exists (ID=251)

Is there something I am missing here?

Also what is the best way to troubleshoot REST upload issues on the SharePoint server (we are on-premises)?

function fileUpload()
    {
    // User does not have to select a file:

    if (document.getElementById("fileSelector").files.length === 0) {

alert('New template requested!\n\nPress OK to continue');

       return;

    }

    var parts = document.getElementById("fileSelector").value.split("\\");
    var filename = parts[parts.length - 1];
    var file = document.getElementById("fileSelector").files[0];
    var listName ="NewTemplateRequest";

    // Get the file:
    var getFileBuffer = function(file) 
    {
    var deferred = $.Deferred();
    var reader = new FileReader();   
    reader.onload = function(e) 
    {
        deferred.resolve(e.target.result);
    }
    reader.onerror = function(e) 
    {
        deferred.reject(e.target.error);
    }
    reader.readAsArrayBuffer(file);
    return deferred.promise();
    };

    getFileBuffer(file).then(function(buffer) {
     stgNextID="251";

   $.ajax({
    url: "internal url/_api/web/lists/GetByTitle('"+listName+"')/items("+stgNextID+")/AttachmentFiles/add(FileName='" + file.name + "')",

    method: 'POST',
    data: buffer,
    processData: false,    
    success: fsucc,
    error: ferr,
    headers: 
    {
        "Accept": "application/json; odata=verbose",
        "content-type": "application/json; odata=verbose",
        "X-RequestDigest": document.getElementById("__REQUESTDIGEST").value,
        "content-length": buffer.byteLength
    }
    });    
  });  
  function fsucc(data) 
    {           
        alert('New template requested!\n\nPress OK to continue');    
    }

    function ferr(data) 
    {
        alert('An error has occurred.\n\nPlease report this to the SharePoint team:\n\n' + data.statusText + '\n\n' + data.responseText);
    }       
}
  • PS I have checked Central Admin and IIS to ensure the max upload file sizes are 250MB. Also, on further testing the issue is intermittent - sometimes it uploads a > 5MB file and sometimes not. The only other thing I can think may be a factor is that the list I am uploading to has a workflow triggered on document creation - could this be an issue? Note: Within the code I first create a list items and then attach the uploaded file to it. – Jimmy Sep 9 '16 at 19:29
  • Also, this is not a cross domain call – Jimmy Sep 9 '16 at 20:32
  • Where have you got the page with upload sitting, is it in SharePoint or is it on a separate IIS box? – Cthulhubutt Sep 12 '16 at 9:32
  • Sorry - got side-tracked at work - In answer to your question, it's on SharePoint. – Jimmy Sep 20 '16 at 20:19

Your Answer

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

Browse other questions tagged or ask your own question.