简体   繁体   English

类型错误:使用 pnpjs 在 SharePoint 中上传文件时无法获取

[英]Typeerror : failed to fetch while Uploading file in SharePoint using pnpjs

I have written below code in SPFx Webpart to upload file to Sharepoint Library.我在 SPFx Webpart 中编写了以下代码以将文件上传到 Sharepoint 库。 It gives me error sometimes like TypeError : Failed to fetch.它有时会给我错误,例如 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();
    }

Looks like you have not initialized the pnpjs (sharepoint) library.看起来您还没有初始化 pnpjs (sharepoint) 库。 The error "failed to fetch" usualy means there is a CORS request rejection.错误“无法获取”通常意味着存在 CORS 请求拒绝。 Check out the "getting started" document: https://pnp.github.io/pnpjs/getting-started/查看“入门”文档: https : //pnp.github.io/pnpjs/getting-started/

class HelloWorldWebPart {
  ....
  public onInit() {
    ...
    sp.setup(this.context);  //< this part seems to be missing?

声明:本站的技术帖子网页,遵循CC BY-SA 4.0协议,如果您需要转载,请注明本站网址或者原文地址。任何问题请咨询:yoyou2525@163.com.

 
粤ICP备18138465号  © 2020-2024 STACKOOM.COM