如何将原始jpeg数据处理为JavaScript中的文件 [英] How to process raw jpeg data into a file in javascript

查看:113
本文介绍了如何将原始jpeg数据处理为JavaScript中的文件的处理方法,对大家解决问题具有一定的参考价值,需要的朋友们下面随着小编来一起学习吧!

问题描述

我正在尝试根据javascript中的原始数据创建jpeg文件

我一直在使用google file picker api来允许用户从驱动器中选择图像并将其导入到我的应用程序中,但是当我发出下载图像的请求时,google会返回原始数据,如下所示:

Google的回应

我曾尝试通过以下方式将这些数据放入Blob:

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

我还尝试过将数据放入文件中.

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

但是,该文件仅呈现黑色像素.如何从该数据生成jpeg文件,以便可以将其上传到服务器?

更新(解决方案): 我将请求api切换为访存并在响应中使用.blob()

解决方案

有关超级代理,请参见 http://visionmedia.github.io/superagent/#parsing-response-body ->二进制和 http://visionmedia.github.io/superagent/#response-properties ->响应正文.

您需要使用.responseType('blob')res.body.

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

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

解决方案

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

这篇关于如何将原始jpeg数据处理为JavaScript中的文件的文章就介绍到这了,希望我们推荐的答案对大家有所帮助,也希望大家多多支持IT屋!

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