如何使用 NodeJS v18 将文件 xml 上传到 SharePoint Online?

问题描述 投票:0回答:1

我手爱特上传文件。在在线共享点的文件夹中的 web.xml 中,我只有 client_id、client_secret 和我的共享点的 url。不幸的是,我找不到解决方案。

我尝试使用 pnp 执行此操作,但该版本与 Nodejs 18 不兼容,并且我无法使用版本 3 和 4 执行此操作。我找不到其他允许我执行此操作的方法或库。我想将我的文件上传到我的共享点文件夹。目前没有什么有效的

javascript node.js xml sharepoint pnp-js
1个回答
0
投票

以下是如何上传文件的基本示例:

const { ClientSecretCredential } = require('@azure/identity');
const { Client } = require('@microsoft/microsoft-graph-client');

const clientId = 'YOUR_CLIENT_ID';
const clientSecret = 'YOUR_CLIENT_SECRET';
const tenantId = 'YOUR_TENANT_ID';
const siteId = 'YOUR_SITE_ID'; // This is the ID of your SharePoint site
const targetFolderId = 'YOUR_TARGET_FOLDER_ID'; // This is the ID of the folder where you want to upload the file
const filePath = 'PATH_TO_YOUR_XML_FILE'; // Path to the XML file you want to upload

const credential = new ClientSecretCredential(tenantId, clientId, clientSecret);
const client = Client.initWithMiddleware({
    authProvider: {
        getAccessToken: async () => {
            const token = await credential.getToken('https://graph.microsoft.com/.default');
            return token.token;
        },
    },
});

async function uploadFile() {
    try {
        const fileContent = fs.readFileSync(filePath);
        const fileName = path.basename(filePath);
        const response = await client.api(`/sites/${siteId}/drive/items/${targetFolderId}:/${fileName}:/content`)
            .put(fileContent);
        console.log('File uploaded successfully:', response);
    } catch (error) {
        console.error('Error uploading file:', error);
    }
}

uploadFile();
© www.soinside.com 2019 - 2024. All rights reserved.