0

The code was working perfectly and suddenly this error:

"Request failed with error message - File Not Found. Stack Trace - undefined"

comes out, can some please help me?

onFileSelected(event)
{
    this.filename = event.target.files[0].name;
    this.selectedFile = event.target.files[0];
    const reader = new FileReader();
    reader.onload = (e) =>
    {
        this.upload(reader.result, this.filename);
    };
    reader.onerror = (e) =>
    {
        alert(reader.error);
    };
    reader.readAsArrayBuffer(this.selectedFile);
}

upload(aB, fileName)
{
    const bytes = new Uint8Array(aB);
    let i;
    let length;
    let out = '';
    for (i = 0, length = bytes.length; i < length; i += 1)
    {
      out += String.fromCharCode(bytes[i]);
    }
    this.base64 = btoa(out);
    console.log(this.base64);
}

uplaodFile()
{
    // this.spinner.show();
    let attachmentFiles;
    // @ts-ignore
    const clientContext = new SP.ClientContext(`https://${window.location.host}/sites/spec-dev/portal/`);
    let oWeb = clientContext.get_web();

    // Get list and Attachment folder where the attachment of a particular list item is stored
    const oList = oWeb.get_lists().getByTitle('Reviews');
    const attachmentFolder = oWeb.getFolderByServerRelativeUrl('Lists/Reviews/Attachments/' + this.all[0].ID);
    // @ts-ignore
    const createInfo = new SP.FileCreationInformation();
    createInfo.set_content(this.base64);
    createInfo.set_url(this.filename);
    createInfo.set_overwrite(true);

    attachmentFiles = attachmentFolder.get_files().add(createInfo);

    clientContext.load(oList);
    clientContext.load(attachmentFiles);
    clientContext.executeQueryAsync(this.QuerySuccess, this.QueryFailure);
}

This is code I am using to upload file to SharePoint List (Attachment).

  • how is this all[]-array filled / what does it contain? How do you check that the ID you are using in the getFolderByServerRelativeUrl call still exists? Error message looks like that the item with a certain ID cannot be found... – Celophysis Oct 11 at 7:52
  • Thanks man, the id was the issue – Harrish Oct 14 at 5:02
  • Hi @Celophysis the actual issue is with the attachment folder in Sharepoint, the error was shown because tat item in the list didn't have the attachment folder yet created, It seems like at least one attachment has to be uploaded to create the attachment folder in Sharepoint. – Harrish Oct 14 at 7:05

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.