1

I want to Upload a file inside a folder using the REST API. Also if the Folder which I have passed is not there then we need to create the folder and then upload the file. Is this really achievable?

Your feedback and suggestions are much appreciated.

0

These snippets should help you I guess. It's done for images but you can easyly adapt it to other fileformats I hope

/**
   * create attachment for an Listitem from a imageURL
   * @param item where the image will be attached
   * @param imageName filename of image
   * @param url image source
   **/
  private _appendImage(item, imageName, url): Promise<any> {
    debugger;
    return this._getArrayBufferImage(url).then((result) => {
      console.log(result);
      return item.attachmentFiles.add(imageName, result).then(v => {
        console.log(v);
      });
    });
  }

and

/**
   * fetches a image from an URL as ArrayBuffer
   * @param imageUrl image source
   */
  private async _getArrayBufferImage(imageUrl: string): Promise<any> {
    var res = await fetch(imageUrl, {
      credentials: 'same-origin'
    });
    var blob = await res.blob();
    return new Promise((resolve, reject) => {
      var reader = new FileReader();
      reader.addEventListener("load", () => {
        resolve(reader.result);
      }, false);
      reader.onerror = () => {
        return reject(this);
      };
      //reader.readAsDataURL(blob);
      reader.readAsArrayBuffer(blob);
    });

  }

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.