How to sending DOM to Image in server(Vuejs)? - vue.js

Please can anyone me show how to sending Base64 image into server,I am using Dom to image library.
//My script
import domtoimage from "dom-to-image";
export default {data: function() {return{ post: { image: "" },}
createPost(post) {
var node = document.getElementById("my-node");
domtoimage.toPng(node).then(function(dataUrl) {
var image = new Image();
image.src = dataUrl;
document.body.appendChild(image); }) this.$store.dispatch('createPost', post)},}
//In Action.js
createPost({commit}, post) {axios.post('posts', post).then(res => {commit('CREATE_POST', res.data)})},

imgProfile(event) {
this.image = event.target;
if (this.image.files && this.image.files[0]) {
var reader = new FileReader();
reader.onload = (e) => {
this.imageData = e.target.result;
}
reader.readAsDataURL(this.image.files[0]);
this.SvImage = this.image.files[0];
}
},

Related

FFmpegKit create, load, fetchFile in react native

I was using ffmpeg.min.js through <script src='https://unpkg.com/#ffmpeg/ffmpeg#0.9.6/dist/ffmpeg.min.js'></script>
async function mergeVideo(video, audio) {
let { createFFmpeg, fetchFile } = FFmpeg;
let ffmpeg = createFFmpeg();
await ffmpeg.load();
ffmpeg.FS('writeFile', 'video.mp4', await fetchFile(video));
ffmpeg.FS('writeFile', 'audio.mp4', await fetchFile(audio));
await ffmpeg.run('-i', 'video.mp4', '-i', 'audio.mp4', '-c', 'copy', 'output.mp4');
let data = await ffmpeg.FS('readFile', 'output.mp4');
return new Uint8Array(data.buffer);
};
function saveVideo(fileName, byte) {
var blob = new Blob([byte], { type: "video/mp4" });
var link = document.createElement('a');
link.href = window.URL.createObjectURL(blob);
link.download = fileName;
link.click();
};
mergeVideo("/videoplayback.webm", "/videoplayback2.webm").then(r => {
saveVideo("mergedVideo.mp4", r);
});
I was using above code for website but i am not getting how to do it in react native.
I did tried FFmpegKit its different there.
Can someone code similar to that in react native?

upload a pdf to s3 from frontend -> node js -> s3

frontend app:
const readURL = (input) => {
if (input.files && input.files[0]) {
let reader = new FileReader();
reader.fileName = input.files[0].name;
reader.onload = async function (e) {
uploadPhoto(reader, e);
};
reader.readAsDataURL(input.files[0]);
}
};
const uploadPhoto = (reader, e) => {
let client = new ServerData();
client.put("/images/upload", {
imageBase64: reader.result,
name: e.target.fileName,
typeOfUpload: "xxxx-bank",
}).then(uploadResult => {
....
})
};
backend node.js
fileContent = base64Image // directly from frontend
fileContent = Buffer.from(base64Image,'base64'); //tried this as well
let params = {
Bucket: 'bucket',
Key: 'name.pdf',
Body: fileContent,
ContentEncoding: 'base64',
ACL: 'private'
}
let upload = new AWS.S3.ManagedUpload({
params: params
});
notice the fileContent
for images it works and i'm using
Buffer.from(base64Image.replace(/^data:image\/\w+;base64,/, ""),'base64');
the solution was
Buffer.from(base64Image.replace(/^data:.+;base64,/, ""),'base64');

How to send image on WEBrtc channel?

I am stuck on sending image via webrtc channel, but I got this error.
Cannot read property 'files' of undefined at SimpleWebRTC
I need help. Thank you in advance.
$("#send-image").change(function() {
if (this.files && this.files[0]) {
var reader = new FileReader();
reader.onload = function(e) {
$('.output #photo').attr('src', e.target.result);
}
reader.readAsDataURL(this.files[0]);
webrtc.sendDirectlyToAll('shareImage', '');
}
});
var input = $('#send-image')[0];
if (input.files[0]) {
var reader = new FileReader();
reader.onload = function(e) {
$('.output #photo').attr('src', e.target.result);
}
reader.readAsDataURL(input.files[0]);
}

Image upload using react-admin

I am new to react-admin. I am using react-admin to upload the file. I have following the step mentioned below in tutorial.
But after I submit the request...I see http trace as follow. I see blob link instead of Base64 image payload.
{
"pictures": {
"rawFile": {
"preview": "blob:http://127.0.0.1:3000/fedcd180-cdc4-44df-b8c9-5c7196788dc6"
},
"src": "blob:http://127.0.0.1:3000/fedcd180-cdc4-44df-b8c9-5c7196788dc6",
"title": "Android_robot.png"
}
}
Can someone please advice how to get base64 image payload instead of link?
Check to see if you have this handler, most likely you did not change the name of the resource posts to your:
const addUploadCapabilities = requestHandler => (type, resource, params) => {
if (type === 'UPDATE' && resource === 'posts') {
Create your custom dataProvider to convert picture to base64
import restServerProvider from 'ra-data-json-server';
const servicesHost = 'http://localhost:8080/api';
const dataProvider = restServerProvider(servicesHost);
const myDataProfider = {
...dataProvider,
create: (resource, params) => {
if (resource !== 'your-route' || !params.data.pictures) {
// fallback to the default implementation
return dataProvider.create(resource, params);
}
const myFile = params.data.pictures;
if ( !myFile.rawFile instanceof File ){
return Promise.reject('Error: Not a file...'); // Didn't test this...
}
return Promise.resolve( convertFileToBase64(myFile) )
.then( (picture64) => ({
src: picture64,
title: `${myFile.title}`
}))
.then( transformedMyFile => dataProvider.create(resource, {
...params,
data: {
...params.data,
myFile: transformedMyFile
}
}));
}
};
const convertFileToBase64 = file => new Promise((resolve, reject) => {
const reader = new FileReader();
reader.readAsDataURL(file.rawFile);
reader.onload = () => resolve(reader.result);
reader.onerror = reject;
});
export default myDataProfider;
And get image data at your Server API
exports.create = (req, res) => {
if(req.body.myFile){
var file = req.body.myFile;
var fs = require('fs');
var data = file.src.replace(/^data:image\/\w+;base64,/, "");
var buf = Buffer.from(data, 'base64');
fs.writeFile(`upload/${file.title}`, buf, err => {
if (err) throw err;
console.log('Saved!');
});
}};

VueJS FileReader

I'm trying to use ExcelJS in Vue and I need FileReader to read and parse the files but I'm getting errors. How do I use FileReader with VueJS?
Input Form
<input type="file"
id="importProductionSchedule"
name="importProductionSchedule"
#change="checkFile($event)"
ref="importProductionSchedule"
>
checkFile method
checkFile() {
let reader = new FileReader()
let self = this
reader.onload = (e) => {
let bstr = e.target.result
let wb = XLSX.read(bstr, {type:'binary'})
let wsname = wb.SheetNames[0]
let ws = wb.Sheets[wsname]
let data = XLSX.utils.sheet_to_json(ws, {header:1})
self.form.filedata = data
self.cols = make_cols(ws['!ref'])
}
reader.onerror = (stuff) => {
console.log("error", stuff)
console.log (stuff.getMessage())
}
// reader.readAsArrayBuffer(event)
reader.readAsBinaryString(event.target.files[0])
},
First of all, logging event.target.files[0] in the console would return the file, but I'm testing both event and event.target.files[0] to make sure.
These are my errors:
event = Uncaught Error: cannot read as File: {"isTrusted":true}
event.target.files[0] = Uncaught Error: cannot read as File: {}
you can use below method
createImage(file) {
let reader = new FileReader()
reader.onload = (event) => {
this.product.image = event.target.result
}
reader.readAsDataURL(file)
}
methods:{
uploadImage(event) {
const image = event.target.files[0];
const reader = new FileReader();
reader.readAsDataURL(image);
reader.onload = (event) => {
this.previewImage = event.target.result;
};
},
}