Retrieving a video file from the input and showcasing it using Typescript

Currently, I have implemented this code in order to retrieve an image that has been "uploaded" into the browser using the <input type="file"> tag, and then sending the data to a component that will utilize it.

 fileReady(e) {
    let file: File = e[0];
    let image = new Image();
    let src: string;

    image.onload = function () {
      src = image.src;
      //do something with image

    }.bind(this);
    src = URL.createObjectURL(file);
    image.src = src;
  }

While this method works perfectly for images, my next objective is to extend its capability to handle videos as well. Currently, neither the Video() constructor nor the HTMLVideoElement seem to achieve this goal. How can I adjust this code to accommodate videos?

Answer №1

Here is an example:

handleFile(e) {
      const fileContent: File = e[0];
      const fileReader = new FileReader();
      fileReader.onloadend = (event: any) => {
        const buffer = event.target.result;
        const fileType = "video/mpeg";
        const mediaBlob = new Blob(buffer , {type: fileType });
        const src = URL.createObjectURL(mediaBlob);
        video.src = src;
      };
      fileReader.readAsArrayBuffer(fileContent);
    }

Answer №2

Adding on to the most recent update in the solution:

const blob = new Blob(arrayBuffer , {type: fileType });

An error is thrown stating that the Blob requires an iterable input. The resolution is as follows:

const blob = new Blob([arrayBuffer], {type: fileType });

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

Passing parameters to an Angular CLI ejected app

Currently, I am utilizing @angular/[email protected]. I have leveraged the new eject feature to modify the webpack configuration. Now, I find myself perplexed on how to pass parameters to my build process. For instance, how can I execute ng build --p ...

Implementing queuing functionality in Angular's HttpClient

I am facing a similar requirement that was discussed in Add queueing to angulars $http service, but I am looking for an implementation in Angular 4.3 or 5 using the HttpInterceptor from @angular/common/http. The API I am working with has some unique limit ...

Converting JSONSchema into TypeScript: Creating a structure with key-value pairs of strings

I am working with json-schema-to-typescript and looking to define an array of strings. Currently, my code looks like this: "type": "object", "description": "...", "additionalProperties": true, "items": { "type": "string" ...

Can you provide the syntax for a generic type parameter placed in front of a function type declaration?

While reviewing a project code recently, I came across some declarations in TypeScript that were unfamiliar to me: export interface SomeInterface<T> { <R>(paths: string[]): Observable<R>; <R>(Fn: (state: T) => R): Observable ...

Filter array to only include the most recent items with unique names (javascript)

I'm trying to retrieve the most recent result for each unique name using javascript. Is there a straightforward way to accomplish this in javascript? This question was inspired by a similar SQL post found here: Get Latest Rates For Each Distinct Rate ...

The 'Access-Control-Allow-Origin' header can be found on the resource that was requested

I'm facing an issue while trying to connect my .net core API to an Angular application. Whenever I attempt to do so, I encounter the following error: Access to XMLHttpRequest at 'https://localhost:44378/api/recloadprime' from origin 'h ...

Ensure to pass the correct type to the useState function

I have a basic app structured like this import React, { useState } from 'react' import AddToList from './components/AddToList' import List from './components/List' export interface IProps{ name: string age: number url: ...

Angular - Automatically update array list once a new object is added

Currently, I'm exploring ways to automatically update the ngFor list when a new object is added to the array. Here's what I have so far: component.html export class HomePage implements OnInit { collections: Collection[]; public show = t ...

The pre-line white-space property is not functioning as anticipated in my CSS code

content: string; this.content = "There was an issue processing your request. Please try using the browser back button."; .content{ white-space: pre-line; } <div class="card-body text-center"> <span class="content"> {{ content }} </span& ...

Guide to Making a Basic TypeScript Metadata Tag in Your Code

I'm looking for a way to format certain fields before sending them to the server-side. My goal is to serialize specific fields of my TypeScript classes using custom serializers. An example of what I'm aiming for is as follows: export class Pers ...

What are the reasons for being unable to utilize display flex within Angular?

Recently delving into Angular, I have been trying to apply display flex to <mat-form-field> without success. I have scoured Google for answers, but still haven't found a solution. Here is my HTML: <form [formGroup]="searchForm" (n ...

Iterating through an array and setting variables according to asynchronous code

I have created a function to loop through an array, call a promise, and update a variable based on the result. The code seems to be functioning correctly, but I am wondering if there is a more optimal way to write it. Any suggestions are appreciated. Tha ...

Is there a method to categorize an array of objects by a specific key and generate a new array of objects based on the grouping in JavaScript?

Suppose I have an array structured like this. It contains objects with different values but the same date. [ { "date": "2020-12-31T18:30:00.000Z", "value": 450 }, { "date": "20 ...

By implementing a custom function within the router's "redirectTo" method, we can dynamically determine the destination for redirection, effectively avoiding Ahead-of-Time (A

By implementing a function to decide where the user should be directed when the site loads, I encounter the following challenge: { path : '', redirectTo: redirector(), pathMatch: 'full' } The redirector() function returns a rout ...

Implementing a default child route in Nativescript

Is there a way for me to access /account/dashboard while the router is set to '/account'? My current routes do not seem to be working properly. The AccountPage component is loading instead of the AccountDashboardPage. export const routes = [ ...

Is it possible to use TypeScript in a React Native project with a JavaScript file?

Currently, I am learning React Native by working on app clones like Instagram and YouTube. I have recently started an AirBnb clone project, but I'm facing some issues with the initial build. One issue I noticed is that in 'App.js', the temp ...

What led the Typescript Team to decide against making === the default option?

Given that Typescript is known for its type safety, it can seem odd that the == operator still exists. Is there a specific rationale behind this decision? ...

Step-by-step guide to designing a leaflet map using Angular Formly

I am faced with a challenge to incorporate a leaflet map into an angular form using formly, and being new to this formly framework is making it difficult for me. Previously, I was able to integrate the map with regular HTML in angular as shown below: map ...

An Unexpected ER_BAD_FIELD_ERROR in Loopback 4

I encountered an unusual error: Unhandled error in GET /managers: 500 Error: ER_BAD_FIELD_ERROR: Unknown column 'role_id' in 'field list' at Query.Sequence._packetToError (/Users/xxxx/node_modules/mysql/lib/protocol/se ...

What is the best way to implement Angular translation for multiple values in a typescript file, while also incorporating parameters?

this.snackBar.open( `Only files of size less than ${this.fileSizeAllowed}KB are allowed`, this.translate.instant('USER_REG.close'), { panelClass: 'errorSnackbar', ...