通过 api 图形将图像文件上传到共享点 [英] upload an image file to sharepoint via api graph

查看:62
本文介绍了通过 api 图形将图像文件上传到共享点的处理方法,对大家解决问题具有一定的参考价值,需要的朋友们下面随着小编来一起学习吧!

问题描述

我正在尝试将一个图像文件从我的 node.js 应用程序上传到 Sharepoint 中的组驱动器.

I am trying to upload an image file from my node.js application to a group's drive in Sharepoint.

作为 官方文档 状态,我提出我的请求如下:

As the official documentation states, I'm making my request as follows:

PUT /groups/{group-id}/drive/items/{parent-id}:/{filename}:/content

正文中带有二进制图像:请求正文的内容应该是要上传的文件的二进制流."

With the binary image in the body: "The contents of the request body should be the binary stream of the file to be uploaded."

问题是图像已上传,但文件已损坏.我尝试了不同的解决方案,但仍然不明白为什么图像总是损坏.

The problem is that the image is uploaded but as a corrupted file. I tried different solutions and still don't see why is always the image corrupted.

这是我的代码:

//i get my image from a URL first
https.get(url.parse(attachment.contentUrl), function (response) {
    var data = [];
    response.on('data', function (chunk) {
        data.push(chunk);
    });
    response.on('end', function () {
      if (response.statusCode === 200) {
        var buffer = Buffer.concat(data);
        //store my image in a local file to test if image is correct (which it is)
        fs.writeFile(localFileName, buffer, (fsError) => {
            //error handling
        });
        functions.uploadImageToSharepoint(session, localFileName, buffer, 
            function (err, body, res) {
                if (err) {
                    console.error(err);
                }else{
                    console.log('OK!');
                }
            });
      } else {
        //error handling
      }
    });
}).on('error', function (e) {
    console.log("error2: " + e);
});

//and the request to graph api
function uploadImageToSharepoint(session, fileName, data, callback) {
  var options = {
    url: 'https://graph.microsoft.com/v1.0/groups/xxxxxxx/drive/root:/yyyyyy/fileName.jpg:/content', 
    method: 'PUT',
    body: data,
    json: true,
    headers: {
      'Content-Type': 'image/jpg',
      Authorization: 'Bearer ' + session.userData.accessToken
    }    
  };

  request(options, function (err, res, body) {
    if (err) return callback(err, body, res);
    if (parseInt(res.statusCode / 100, 10) !== 2) {
        if (body.error) {
            return callback(new Error(res.statusCode + ': ' + (body.error.message || body.error)), body, res);
        }
        return callback(err, body, res);
    }
    callback(err, body ,res);   
  });  
}

推荐答案

由于以下请求选项,文件很可能已损坏:

The file is most likely getting corrupted due to the following option for request:

var options = {
    json: true,   //<--setting this option sets body to JSON representation of value
    //another properties are omitted for clarity
  };

在这种情况下,request 将正文设置为值的 JSON 表示并添加 accept上传端点和二进制文件损坏.

In that case request sets body to JSON representation of value and adds accept header to application/json for Upload endpoint and binary file get corrupted.

解决方案是从请求中省略 json 选项并仅使用正确的 content-type:

The solution would be to omit json option from a request and use the proper content-type only:

var options = {
    url: '/me/drive/root:/filename.jpg:/content', 
    method: 'PUT',
    body: data,
    headers: {
      'Content-Type': 'image/jpg',
      Authorization: 'Bearer ' + accessToken
    }    
};

这篇关于通过 api 图形将图像文件上传到共享点的文章就介绍到这了,希望我们推荐的答案对大家有所帮助,也希望大家多多支持IT屋!

查看全文
登录 关闭
扫码关注1秒登录
发送“验证码”获取 | 15天全站免登陆