'how to convert binary image response into jpg
I'm working with a project, where I will have to download the attachments by sending a request, here is the image file.
the response from an api to download image gives me this binary data. How can I change this to JPG so that the image will be downloaded as an image.
{data: '����\x00\x10JFIF\x00\x01\x01\x00\x00\x01\x00\x01\x00\x00��\x00�\x00\t\x06\x07\b\x10\r\t\b\b\x10\n\n\t\b\x07\n\x07\x07\x07\x07\r\x0F\n\n\b…»�Q��\x1E�P��\x05i\x15�m��[��>�Y_P��A\x01\x01\x01\x01\x01\x01\x01\x01\x01\x01\x01\x01\x01\x01\x01\x01\x01\x01\x07��', status: 200, statusText: 'OK', headers: {…}, config: {…}, …}
Expanded
{data: '����\x00\x10JFIF\x00\x01\x01\x00\x00\x01\x00\x01\x00\x00��\x00�\x00\t\x06\x07\b\x10\r\t\b\b\x10\n\n\t\b\x07\n\x07\x07\x07\x07\r\x0F\n\n\b…»�Q��\x1E�P��\x05i\x15�m��[��>�Y_P��A\x01\x01\x01\x01\x01\x01\x01\x01\x01\x01\x01\x01\x01\x01\x01\x01\x01\x01\x07��', status: 200, statusText: 'OK', headers: {…}, config: {…}, …}
config:
adapter: ƒ xhrAdapter(config)
data: "{\"account\":\"[email protected]\",\"messageId\":\"17fc047c3b3a9c14\",\"attachment\":{\"filename\":\"images (1).jpeg\",\"mimeType\":\"image/jpeg\",\"attachmentId\":\"ANGjdJ-pgFSJZhgsuct_xW4M-hJ-5R_u-dw54lGFSF9tn32w1eDBuXuQ4OLD_kjMNiubz-LVbH-4p2tw-voawHiV9nnhOdZS0sEDy06ZNgMMcRHuxUi1v942lsp217Ej8BKH8muOJriDbxVLRw-S-c415tkpDmW-FgOccDvxmu6vVCR2Id7q0NNcyrhj19xPDFazTrJXfu7_vKkoIrSwcJLZZxSzBrqLEhtBHcP_XG5O7KKLU2G-5-h2of8LzmjX5D5BnaMzOXYQoi2CiLR0nL1VvJjKBJKoWzS5VlxJqBSCFDcaaFNM81EM6k9Pu35AzTB_1edG5P_kw91FBy-i6S9i2y30Uf7UiWPHpitVfCA4QaL1-3o0xCnw2QfasxokBxDjRtBP-OeV2b-A28PQ\"}}"
headers: {Accept: 'application/json, text/plain, */*', Content-Type: 'application/json', Authorization: 'JWT eyJhbGciOiJSUzI1NiIsInR5cCI6IkpXVCJ9.eyJpZCI6I…fuIiwfb9qGxbQAJDSikh-ym6JUvkiS6g71N4E_tvGtJpZxZUw'}
maxBodyLength: -1
maxContentLength: -1
method: "post"
timeout: 0
transformRequest: [ƒ]
transformResponse: [ƒ]
transitional: {silentJSONParsing: true, forcedJSONParsing: true, clarifyTimeoutError: false}
url: "http://172.104.48.145:6001/api/v1/visab/email/getAttachment"
validateStatus: ƒ validateStatus(status)
xsrfCookieName: "XSRF-TOKEN"
xsrfHeaderName: "X-XSRF-TOKEN"
[[Prototype]]: Object
data: "����\u0000\u0010JFIF\u0000\u0001\u0001\u0000\u000
headers: {content-length: '35320', content-type: 'image/jpeg'}
request: XMLHttpRequest {onreadystatechange: null, readyState: 4, timeout: 0, withCredentials: false, upload: XMLHttpRequestUpload, …}
status: 200
statusText: "OK"
[[Prototype]]: Object
here is my action file... Action.js
import useJwt from '@src/auth/jwt/useJwt'
import { GET_ATTACHMENT_FAILED, GET_ATTACHMENT_INITIATED, GET_ATTACHMENT_SUCCESS, RESET_EMAILS } from '../ActionTypes/mails'
export const resetAttachment = () => ({ type: RESET_EMAILS })
export const getAttachmentInitiated = () => ({ type: GET_ATTACHMENT_INITIATED })
export const getAttachmentSuccess = data => ({ type: GET_ATTACHMENT_SUCCESS, payload: data })
export const getAttachmentFailed = error => ({ type: GET_ATTACHMENT_FAILED, payload: error })
export const handleGetAttachment = data => {
return async dispatch => {
try {
dispatch(getAttachmentInitiated())
console.log('handleGetAttachment INITIATED: ')
const response = await useJwt.getAttachment(data)
console.log('handleGetAttachment RESPONSE: ', response)
if (response.data) {
dispatch(getAttachmentSuccess(response.data))
console.log('handleGetAttachment SUCCESS:............ ')
}
} catch (err) {
if (err.response) {
dispatch(getAttachmentFailed(err.response.data))
console.log('handleGetAttachment FAILED: ', err.response.data)
}
}
}
}
Thankyou
Sources
This article follows the attribution requirements of Stack Overflow and is licensed under CC BY-SA 3.0.
Source: Stack Overflow
| Solution | Source |
|---|

