How to process raw jpeg data into a file in javascript

ⅰ亾dé卋堺 提交于 2019-12-25 01:32:42

问题


I'm trying to create a jpeg file from raw data in javascript

I've been using the google file picker api to allow users to select their images from drive and import into my application, but when I make the request to download the image google responds with raw data that looks like this:

Google's response

I have tried placing that data into a blob with:

var blob = new Blob([res.text], {type: 'image/jpeg'})

I have also tried placing the data into a file.

var file = new File([res.text], 'image.jpeg', {type:"image/jpeg"})

However the file only renders black pixels. How can I go about generating a jpeg file from that data so I can upload to my server?

Update(Solution): I switched my request api to fetch and used .blob() on the response


回答1:


For superagent see http://visionmedia.github.io/superagent/#parsing-response-bodies -> Binary and http://visionmedia.github.io/superagent/#response-properties -> Response body.

You need to use .responseType('blob') and res.body.

https://stackblitz.com/edit/js-so-how-to-process-raw-jpeg-data-into-a-file-in-javascript



来源:https://stackoverflow.com/questions/56135755/how-to-process-raw-jpeg-data-into-a-file-in-javascript

易学教程内所有资源均来自网络或用户发布的内容,如有违反法律规定的内容欢迎反馈
该文章没有解决你所遇到的问题?点击提问,说说你的问题,让更多的人一起探讨吧!