0

I have written below code in SPFx Webpart to upload file to Sharepoint Library. It gives me error sometimes like TypeError : Failed to fetch. Can anyone give me some idea what this error signifies and how can it be resolved.

function uploadFile(myfile, target) {
    var dfd = $.Deferred();
    sp.web.webs.filter("Title eq 'DMS'").get().then(webData => {
        const _web = Web(webData[0].Url);
        //for files < 10 MB
        if (myfile.size <= 10485760) {
            _web.getFolderByServerRelativeUrl(target).files.add(myfile.name, myfile, true).then((result) => {
                dfd.resolve(result);
            }).catch(err => {
                dfd.reject(err + ". Error in method: uploadFile while uploading File");
            });
        }
        //for files > 10 MB
        else {
            _web.getFolderByServerRelativeUrl(target)
                .files.addChunked(myfile.name, myfile)
                .then(({ file }) => file.getItem()).then((item: any) => {
                    dfd.resolve(item);

                }).catch(err => {
                    dfd.reject(err + ". Error in method: uploadFile while uploading File");
                });
        }
    }).catch(err => {
        dfd.reject(err + ". Error in method: uploadFile while getting web");
    });
    return dfd.promise();
}

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.