Steps to change image string information into a blob object

I'm currently faced with an issue wherein I need to download an image from the server and display it on the client side. Despite successfully downloading the image (confirmed in DevTools > Network > name-of-request > Preview), I'm encountering difficulties displaying the image and finding it within (DevTools > Sources > Page > Top > Localhost).

public async getMedia(id: number | undefined): Promise<Blob | undefined> {
    if(!id || id === 0) { return undefined }
    const response = await this.httpClient.get<any>(
        `/api/advertisements/image/${id}`
    );

    const contentType: string = response.headers['content-type'] || 'image/pjpeg';
    const data: string = response.data
    console.log(data)
    const blob = new Blob([data], { type: contentType })
    const url = window.URL.createObjectURL(blob)
    console.log(url)
    return blob
}  

Console Output:

"

$.' ",#(7),01444'9=82..."

"blob:http://localhost:57087/a0ae04cd-c0a0-4263-a7fb-dab400a802a4"

https://i.sstatic.net/nyDZD.png

Additional Information:

This is the code snippet located on the server.

[Route("image/{id}")] 
[HttpGet] 
public async Task<IHttpActionResult> ImageGet(int id) { 
    
    ....insert download from storage account

    var fileName = Path.GetFileName(url);
    await blockBlob.DownloadToByteArrayAsync(buffer, 0);
    response.Content = new ByteArrayContent(buffer, 0, (int)blockBlob.Properties.Length);
    response.Content.Headers.ContentType = new System.Net.Http.Headers.MediaTypeHeaderValue(System.Web.MimeMapping.GetMimeMapping(fileName));
    response.Content.Headers.ContentDisposition = new System.Net.Http.Headers.ContentDispositionHeaderValue("attachment") { FileName = fileName };
    
    return ResponseMessage(response);

}

In order to verify functionality, I tested by uploading an image from the client using the <input> tab, and then utilizing window.URL.createObjectURL(File). The ObjectURL was successfully created and displayed as expected.

Answer №1

Seems like the response type got mixed up with the location.

/api/advertisements/image/${id}, {} , { responseType: 'blob' }

Similar questions

If you have not found the answer to your question or you are interested in this topic, then look at other similar questions below or use the search

JSON Collaborator Tool

I really enjoy using the online json viewer at jsonviewer.stack.hu Does anyone know of a website that allows for sharing parsed json similar to sites like jsbin.com or jsfiddle.net ...

Changing column sorting type dynamically with a button click using jQuery and DataTables

In the column, I have data arranged as "firstValue secondValue". It is essential for this data to remain in a single column and not split into two separate columns. When sorting is applied, it currently sorts based on both firstValue and secondValue. How ...

Troubleshooting: Issue with AngularJS ng-click functionality within Modal

My Modal doesn't open when the button inside it is clicked. The ng-click function in the js file is not being called without any error messages. However, outside of the Modal, the ng-click works fine. <div class="container-fluid" ng-app="app" ng- ...

Encountering difficulties when attempting to establish a connection with a local CouchDB using

I've been attempting to send POST requests to my database by utilizing the jquery.couch.js jQuery plugin that is included with CouchDB. Unfortunately, I'm facing issues connecting to my database and have been experimenting with the following scri ...

Create a new subclass in NodeJS using the `Object.subClass

Take a look at the source code on Github. It shows peer5.core.dataStructures.DoublyLinkedList = Object.subClass({...}) I searched through the JS Object API and couldn't locate the Object.subClass method. Additionally, I couldn't find any referen ...

Determine the variance between two strings using Jquery

Hello and thank you in advance for your help. I'm facing a basic query here - I have two variables: var x = 'abc'; var y = 'ac'; I am looking to compare the two variables and find the dissimilarity between them, which should be: ...

Adding numerous entries into mongoose

Currently, I am utilizing Node.js for the server side of my application and Angular along with HTML for the client side. The issue I am facing is that after inserting data into Mongoose on the server side, I have to restart the server in order to add new ...

Utilizing a non-Angular node module in Angular: Best practices

One way to generate QR codes in JavaScript is by using the qrcode module, which can be found at this link. In pure JavaScript, it's simple to utilize this module: var QRCode = require('qrcode') QRCode.toDataURL('I am a pony!', f ...

JavaScript threw an error because it encountered an unexpected or invalid token

I can't seem to identify the error in this code. Can someone please help me correct it? document.write(' <div> <a href=http://www.socialsignal.ir style=\'\\padding:5px;text-decoration: none;border: 1px solid #555;ba ...

Update the chat <div> automatically whenever a new message is added to the MySQL database

I have been looking online for a code that can update the chat <div> every time a new message is received from the other user. I have come across mentions of setTimeout() and setInterval(), but I would appreciate advice and assistance from more exper ...

Using an image instead of a checkbox when it is selected (Angular 4/Ionic 3)

Is there a way to swap out the checkbox for an image? I want this image to change dynamically based on whether the checkbox is checked or not. Unfortunately, adding a class doesn't seem to do the trick as it modifies all icons in the same column. The ...

Struggling to establish a functioning proxy in my React and Node application

In the process of developing a react client app with a node.js express backend, I have encountered an issue related to project structure. https://i.sstatic.net/8rID0.png The client app includes a proxy configuration in its package.json file: "proxy": "h ...

Sort the Bootstrap table based on the date column

I recently implemented the Bootstrap table plugin into my project, but I am facing issues with sorting dates correctly. Below is the code snippet I am using: <table class=" table-striped" id="table" data-toggle="table" ...

Extracting data from Material-UI TextField elements in React – a guide

I am currently working on a small app that consists of a Form component, a SubmitButton component, and my parent component, App.js. My goal is to retrieve the values of the 3 fields within the form component when the user clicks the submit button, pass the ...

Issue with parsing JSON values in a Chrome extension

Struggling to retrieve the value of a JSON key, but it keeps returning empty. Check out the code snippet below: let json_=JSON.parse(JSON.stringify(result)); console.log(json_); console.log(json ...

What could be causing the HTTP response Array length in Angular to be undefined?

Currently, I am facing an issue while retrieving lobby data from a Spring Boot API to display it in my Angular frontend. After fetching the data and mapping it into an object array, I encountered a problem where the length of the array turned out to be und ...

Can you explain the process of variable allocation?

I have a query regarding the following JavaScript code snippet. It might be a basic question, but I'm seeking clarification. // 'response' contains JSON data received from the backend: ....then(response => { this.myVar = response.data; ...

The differences between getElementById and getElementByClassName

When using both getElementById and getElementByClassName, I have noticed that getElementById sometimes returns null while getElementByClassName works without any issues. I wonder what might be causing this discrepancy. It is my understanding that getElemen ...

Accessing global variables from an event listener in Angular: A quick guide

I'm currently working on an Angular project that utilizes fabric.js for canvas functionality. However, I've encountered a problem when trying to use event listeners with the canvas object. In the snippet of code below, I'm attempting to acc ...

Tips for creating an array:

Currently, I am working on a project that involves using an array of objects. While I have a good understanding of what an array is and how to create an object, I am struggling with incorporating my student object into the array. In this project, each st ...