Issue with File Upload using FileReader: "DomException: A requested file or directory could not be found at the time an operation was processed

38 views Asked by At

I'm encountering an issue with file uploads with FileReader using the provided code snippet. Most of the time, the code works fine without any issues. However, for some users, files are randomly failing to upload, and I've observed an error message in the console that says "DOMException: A requested file or directory could not be found at the time an operation was processed."

Note: in debug mode I can see fileinput has file value still going into onError. There is no issue with file itself as if user upload file again it gets uploaded.

Here's the relevant code snippet:

//Upload the file.
function uploadFile() {
  // The display name must be unique every time you run the example.
  var fileInput = jQuery('#getFile');
  var newName = jQuery('#displayName').val();

  // Initiate method calls using jQuery promises.
  // Get the local file as an array buffer.
  var getFile = getFileBuffer();

  getFile.done(function (arrayBuffer) {
    // Add the file to the SharePoint folder.
    var addFile = addFileToFolder(arrayBuffer);
    alert('Upload Done');
  });

  getFile.fail(onError);

  // Get the local file as an array buffer.
  function getFileBuffer() {
    var deferred = jQuery.Deferred();
    var reader = new FileReader();
    reader.onloadend = function (e) {
      deferred.resolve(e.target.result);
    }
    reader.onerror = function (e) {
      //HERE ERROR IS COMING
      deferred.reject(e.target.error);
    }
    reader.readAsArrayBuffer(fileInput[0].files[0]);
    return deferred.promise();
  }

  // Add the file to the file collection using AJAX
  function addFileToFolder(arrayBuffer) {
    return jQuery.ajax({
      url: fileCollectionEndpoint,
      type: "POST",
      data: arrayBuffer,
      processData: false,
      headers: {
        "accept": "application/json;odata=verbose",
        "content-length": arrayBuffer.byteLength
      }
    });
  }
}

I've reviewed the code but couldn't identify any obvious issues. Could someone help me understand what might be causing this error and how to resolve it? Additionally, any suggestions for improving the code are appreciated.

1

There are 1 answers

3
Lil_PhyziX On

The error you’re encountering could indicate that a file or directory that the operation expects to access is not available at the time the operation is processed and this may happen due to several reasons which include:

  • The file or directory does not exist at the specified path.
  • The file may have been moved or deleted before the operation could access it.
  • There may be permission issues preventing access to the file or directory.
  • The file reference might be incorrect or outdated.

Now, you can try to resolve the issue by verifying the path of where the file should be uploaded to and making sure that directory where said file should go exists. This may not apply to you, but if there is a permission system in place, check to make sure that the application has the necessary permissions to write and upload the file to said directory.