DZone
Thanks for visiting DZone today,
Edit Profile
  • Manage Email Subscriptions
  • How to Post to DZone
  • Article Submission Guidelines
Sign Out View Profile
  • Post an Article
  • Manage My Drafts
Over 2 million developers have joined DZone.
Log In / Join
Refcards Trend Reports
Events Video Library
Refcards
Trend Reports

Events

View Events Video Library

Zones

Culture and Methodologies Agile Career Development Methodologies Team Management
Data Engineering AI/ML Big Data Data Databases IoT
Software Design and Architecture Cloud Architecture Containers Integration Microservices Performance Security
Coding Frameworks Java JavaScript Languages Tools
Testing, Deployment, and Maintenance Deployment DevOps and CI/CD Maintenance Monitoring and Observability Testing, Tools, and Frameworks
Culture and Methodologies
Agile Career Development Methodologies Team Management
Data Engineering
AI/ML Big Data Data Databases IoT
Software Design and Architecture
Cloud Architecture Containers Integration Microservices Performance Security
Coding
Frameworks Java JavaScript Languages Tools
Testing, Deployment, and Maintenance
Deployment DevOps and CI/CD Maintenance Monitoring and Observability Testing, Tools, and Frameworks

Generative AI has transformed nearly every industry. How can you leverage GenAI to improve your productivity and efficiency?

SBOMs are essential to circumventing software supply chain attacks, and they provide visibility into various software components.

Related

  • How To Validate HTTP Post Request Body - Restful Web Services With Spring Framework | Spring Boot
  • Leveraging Salesforce Using a Client Written In Angular
  • RESTful Web Services With Spring Boot: Reading HTTP POST Request Body
  • AngularJS Vs. ReactJS Vs. VueJS: A Detailed Comparison

Trending

  • MCP Client Agent: Architecture and Implementation
  • Elevating LLMs With Tool Use: A Simple Agentic Framework Using LangChain
  • When Caches Collide: Solving Race Conditions in Fare Updates
  • Deploying LLMs Across Hybrid Cloud-Fog Topologies Using Progressive Model Pruning
  1. DZone
  2. Coding
  3. Frameworks
  4. How to Preview Blobs With HTTP POST and Angular 5

How to Preview Blobs With HTTP POST and Angular 5

In this quick but helpful article, a software architect documents how to upload images to a web page using Angular and HTTP POST.

By 
Suren Konathala user avatar
Suren Konathala
·
Oct. 12, 18 · Tutorial
Likes (2)
Comment
Save
Tweet
Share
73.6K Views

Join the DZone community and get the full member experience.

Join For Free

With Angular, we can call a web service to get an image as a Blob, convert that to an image and display it on a web page. This may sound like a straight and standard use case but I ended up spending a lot of time trying to get this to work and a lot of asking around. This has been one of the main motivations to write this article.

What I Wanted to Do

To begin with, I am building a website that displays thumbnails retrieved from a URL. On clicking on the thumbnail, the full sized image loads in a new page. Like a typical carousel, but the catch is that the thumbnail is generated dynamically and does not load from or stored on the local machine.

Use-case: Image at a URL → Run through a Web service to get thumbnail → Web service responds with a thumbnail of the image → Display the thumbnail on the web page.

The Challenges

  1. Make an HTTP POST to get a blob/image — We need to send some input parameters (image URL, keys) to the web service that validates your input and sends the image as a blob (assuming the external service does the thumbnail generation for you).
  2. Convert JSON/blob to an image/thumbnail — HTML cannot understand and display blob (weird characters) that the service returns.

The Solution

With a combination of Angular and vanilla JavaScript, we can achieve this as below.

service.ts

thumbnailFetchUrl : string = "https://south/generateThumbnail?width=100&height=100";

getBlobThumbnail(): Observable<Blob> {
    const headers = new HttpHeaders({
      'Content-Type': 'application/json',
      'Accept': 'application/json'      
    });
    return this.httpClient.post<Blob>(this.thumbnailFetchUrl,
      {
        "url": "http://acs/Logo.png"
      }, {headers: headers, responseType: 'blob' as 'json' });
  }

component.ts

imageBlobUrl: string | null = null;

getThumbnail() : void {
    this.ablobService.getBlobThumbnail()
      .subscribe((val) => {
          this.createImageFromBlob(val);
        },
        response => {
          console.log("POST - getThumbnail - in error", response);
        },
        () => {
          console.log("POST - getThumbnail - observable is now completed.");
        });
  }

  createImageFromBlob(image: Blob) {
    let reader = new FileReader();
    reader.addEventListener("load", () => {
      this.imageBlobUrl = reader.result;
    }, false);

    if (image) {
      reader.readAsDataURL(image);
    }
  }

Since the Angular service ablobService returns a blob, we create an image from it using createImageFromBlob .

component.html

<h2>Thumbnail imageBlobUrl</h2>
  <img [src]="imageBlobUrl">

Image title

The image/thumbnail will be displayed on the HTML web page. 

References

  • Environment: Angular 5.0.0.
  • Live example and code on GitHub (coming soon).
  • StackOverflow on this issue.
  • Discussion on Angular GitHub about this issue.

If you enjoyed this article and want to learn more about Angular, check out our compendium of tutorials and articles from JS to 8.

AngularJS Web Service POST (HTTP)

Published at DZone with permission of Suren Konathala. See the original article here.

Opinions expressed by DZone contributors are their own.

Related

  • How To Validate HTTP Post Request Body - Restful Web Services With Spring Framework | Spring Boot
  • Leveraging Salesforce Using a Client Written In Angular
  • RESTful Web Services With Spring Boot: Reading HTTP POST Request Body
  • AngularJS Vs. ReactJS Vs. VueJS: A Detailed Comparison

Partner Resources

×

Comments

The likes didn't load as expected. Please refresh the page and try again.

ABOUT US

  • About DZone
  • Support and feedback
  • Community research
  • Sitemap

ADVERTISE

  • Advertise with DZone

CONTRIBUTE ON DZONE

  • Article Submission Guidelines
  • Become a Contributor
  • Core Program
  • Visit the Writers' Zone

LEGAL

  • Terms of Service
  • Privacy Policy

CONTACT US

  • 3343 Perimeter Hill Drive
  • Suite 100
  • Nashville, TN 37211
  • [email protected]

Let's be friends: