return FALSE; $r = well_tag_thread__update(array('id' => $id), $update); return $r; } function well_tag_thread_find($tagid, $page, $pagesize) { $arr = well_tag_thread__find(array('tagid' => $tagid), array('id' => -1), $page, $pagesize); return $arr; } function well_tag_thread_find_by_tid($tid, $page, $pagesize) { $arr = well_tag_thread__find(array('tid' => $tid), array(), $page, $pagesize); return $arr; } ?>javascript - How to convert the string type from an API response to an image file - ����u0000u0010JFIFu0000u0001u0001u0000u0000u
最新消息:雨落星辰是一个专注网站SEO优化、网站SEO诊断、搜索引擎研究、网络营销推广、网站策划运营及站长类的自媒体原创博客

javascript - How to convert the string type from an API response to an image file - ����u0000u0010JFIFu0000u0001u0001u0000u0000u

programmeradmin0浏览0评论

I have used /$value API to get the profile picture of the outlook user. I get an image on running the above API in the rest-client. The content-type of the API is "image/jpg"

But, in Node.js, the response of the API is as follows:

����\u0000\u0010JFIF\u0000\u0001\u0001\u0000\u0000\u0001\u0000\u0001\u0000\u0000��\u0000�\u0000\u0005\u0005\u0005\u0005\u0005\u0005\u0006\u0006\u0006\u0006\b\t\b\t\b\f\u000b\n\n\u000b\f\u0012\r\u000e\r\u000e\r\u0012\u001b\u0011\u0014\u0011\u0011\u0014\u0011\u001b\u0018\u001d\u0018\u0016\u0018\u001d\u0018+"\u001e\u001e"+2*(*2<66<LHLdd�\u

I used 'fs' to create an image file. The code is as follows:

const options = {  
    url: "/$value",
    method: 'GET',
    headers: {
        'Accept': 'application/json',
        'Authorization': `Bearer ${locals.access_token}`,
        'Content-type': 'image/jpg',
    }
};

request(options, (err, res, body) => {  
    if(err){
        reject(err);
    }
    console.log(res);
    const fs = require('fs');
    const data = new Buffer(body).toString("base64");
    // const data = new Buffer(body);
    fs.writeFileSync('profile.jpg', data, (err) => {
        if (err) {
            console.log("There was an error writing the image")
        }
        else {
            console.log("The file is written successfully");
        }
    });
});

The file is written successfully, but the .jpg image file generated is broken. I am unable to open the image. The output of the image file is as follows:

77+977+977+977+9ABBKRklGAAEBAAABAAEAAO+/ve

I have used https://graph.microsoft./beta/me/photo/$value API to get the profile picture of the outlook user. I get an image on running the above API in the rest-client. The content-type of the API is "image/jpg"

But, in Node.js, the response of the API is as follows:

����\u0000\u0010JFIF\u0000\u0001\u0001\u0000\u0000\u0001\u0000\u0001\u0000\u0000��\u0000�\u0000\u0005\u0005\u0005\u0005\u0005\u0005\u0006\u0006\u0006\u0006\b\t\b\t\b\f\u000b\n\n\u000b\f\u0012\r\u000e\r\u000e\r\u0012\u001b\u0011\u0014\u0011\u0011\u0014\u0011\u001b\u0018\u001d\u0018\u0016\u0018\u001d\u0018+"\u001e\u001e"+2*(*2<66<LHLdd�\u

I used 'fs' to create an image file. The code is as follows:

const options = {  
    url: "https://graph.microsoft./beta/me/photo/$value",
    method: 'GET',
    headers: {
        'Accept': 'application/json',
        'Authorization': `Bearer ${locals.access_token}`,
        'Content-type': 'image/jpg',
    }
};

request(options, (err, res, body) => {  
    if(err){
        reject(err);
    }
    console.log(res);
    const fs = require('fs');
    const data = new Buffer(body).toString("base64");
    // const data = new Buffer(body);
    fs.writeFileSync('profile.jpg', data, (err) => {
        if (err) {
            console.log("There was an error writing the image")
        }
        else {
            console.log("The file is written successfully");
        }
    });
});

The file is written successfully, but the .jpg image file generated is broken. I am unable to open the image. The output of the image file is as follows:

77+977+977+977+9ABBKRklGAAEBAAABAAEAAO+/ve
Share Improve this question edited Dec 26, 2017 at 8:35 mega6382 9,39617 gold badges50 silver badges72 bronze badges asked Dec 14, 2017 at 5:16 Dinesh MadanlalDinesh Madanlal 3475 silver badges21 bronze badges 10
  • It says image/jpg. So try writing .jpg instead of .png – vibhor1997a Commented Dec 14, 2017 at 5:20
  • Thanks @vibhor1997a> I have updated the question to .jpg . I get the same response for .jpg as well. – Dinesh Madanlal Commented Dec 14, 2017 at 5:23
  • @DineshKumar youcan save the image to a db and get the url?or save the base64. – zabusa Commented Dec 14, 2017 at 5:25
  • Thanks @zabusa. I am unable to open the image in the local. I think it doesn't make sense in storing a broken file in the db. – Dinesh Madanlal Commented Dec 14, 2017 at 5:27
  • The correct MIME type for .jpg is image/jpeg, someone done goofed. – Phix Commented Dec 14, 2017 at 5:47
 |  Show 5 more ments

3 Answers 3

Reset to default 3

You can do this by streaming the response like this,

request(options,(err,res,body)=>{
  console.log('Done!');
}).pipe(fs.createWriteStream('./profile.jpg'));

https://www.npmjs./package/request#streaming

https://nodejs/api/fs.html#fs_class_fs_writestream

The reason for this is that by default, request will call .toString() on the response data. In case of binary data, like a RAW JPEG, this isn't what you want.

It's also explained in the request documentation (albeit vaguely):

(Note: if you expect binary data, you should set encoding: null.)

Which means that you can use this as well:

const options = {  
  encoding : null,
  url      : "https://graph.microsoft./beta/me/photo/$value",
  method   : 'GET',
  headers  : {
    'Accept'        : 'application/json',
    'Authorization' : `Bearer ${locals.access_token}`,
    'Content-type'  : 'image/jpg',
  }
};

However, streaming is probably still the better solution, because it won't require the entire response being read into memory first.

Request is deprecated. You can do this with axios;

// GET request for remote image in node.js
axios({
  method: 'get',
  url: 'http://example./file.jpg',
  responseType: 'stream'
})
  .then(function (response) {
    response.data.pipe(fs.createWriteStream('ada_lovelace.jpg'))
  });

与本文相关的文章

发布评论

评论列表(0)

  1. 暂无评论