Skip to content

Instantly share code, notes, and snippets.

@coolvasanth
Created April 25, 2017 07:36
Show Gist options
  • Save coolvasanth/8d0b6a4cea480bc7017bce0ba3ec5bdb to your computer and use it in GitHub Desktop.
Save coolvasanth/8d0b6a4cea480bc7017bce0ba3ec5bdb to your computer and use it in GitHub Desktop.
uploading video which is captured from device to server via API (works both on android and ios)
//install media capture, file chooser, file transfer, camera plugins from ionic 2 native plaugins and don't forget to add providers in
app.component.ts
import { Component } from '@angular/core';
import { NavController, NavParams,ActionSheetController } from 'ionic-angular';
import { MediaCapture, MediaFile, CaptureError, CaptureImageOptions,CaptureVideoOptions } from '@ionic-native/media-capture';
import { Camera, CameraOptions } from '@ionic-native/camera';
import { Transfer, FileUploadOptions, TransferObject } from '@ionic-native/transfer';
import { FileChooser } from '@ionic-native/file-chooser';
export class Videorupload {
constructor(public navCtrl: NavController, public navParams: NavParams,
private mediaCapture: MediaCapture,
private transfer: Transfer,
private camera: Camera,
) {}
capturevideo()
{
let options: CaptureVideoOptions = { limit: 1 };
this.mediaCapture.captureVideo(options)
.then((videodata) => {
// videodata is either a base64 encoded string or a file URI
// If it's base64:
alert("VIDEO DATA IS"+videodata)
// this.items = videodata;
this.items = JSON.stringify(videodata);
this.items.substring(2);
this.items.substring(0, this.items - 2);
// this process is carried out for extracting the extension of video (it will be MOV in ios mp3, mp4 etc in android)
let x = this.items.split(",");
let sec = x[1].slice(12);;
sec = sec.slice(0,-1)
// sec.toString().substring(12);
// sec.slice(0, -1);
console.log("SECOND IS",sec);
console.log("SECOND LENGTH IS",sec.length);
console.log("VIDEO DATA IS"+ JSON.stringify(videodata))
let y = x[x.length - 1];
let z = y.split(".")
let a = z[1];
a = a.slice(0,-3);
let filename = "name" +"."+a;
console.log("FINALL FORMAT IS",a);
const fileTransfer: TransferObject = this.transfer.create();
let options1: FileUploadOptions = {
fileKey: 'image_upload_file',
fileName: filename,
headers: {},
params: {"app_key":"Testappkey"},
chunkedMode : false
}
fileTransfer.upload(sec, 'API that can take the video', options1)
.then((data) => {
// success
alert("success"+JSON.stringify(data));
}, (err) => {
// error
// alert("error"+JSON.stringify(err));
});
});
}
}
Sign up for free to join this conversation on GitHub. Already have an account? Sign in to comment