I'm having troubles saving an incoming webm blob to the server. I'm using react-video-recorder on NextJS like this:
<VideoRecorder
onRecordingComplete={(videoBlob) => {
// Do something with the video...
fetch("/api/video",
method: "post",
body: videoBlob,
})
.then(function (response) {
console.log("done");
return response;
})
.catch(function (err) {
console.log('error', err);
});
console.log(url);
// output: blob:http://localhost:3000/99a5b711-f3d5-481d-9e04-8981d1be3727
console.log(videoBlob);
// output BlobĀ {size: 307028, type: "video/webm;codecs="vp8,opus""}
}}
/>
On the api side I'm trying to save the file like this. It does save something, but is only the first chunk or buffer. How can I capture and write the file to my server?
export default async (req, res) => {
fs.writeFile('test.webm', req.body, function (err) {
if (err) return console.log(err);
console.log('video saved');
} );
}
I did that task by doing this.
I saved the recordingChunks/Video blob to a state and then sent it to the Nodejs server from Reactjs Frontend
FrontEnd code:-
const blob = new Blob(context.data.recordedChunks, {
type: "video/webm",
});
const fd = new FormData()
fd.append('video', blob)
axios.post(`${process.env.REACT_APP_BASE_URL}/video/blob_video`, fd)
.then((res) => console.log(res.data))
.catch((err) => console.log(err))
Backend code:-
router.post('/blob_video', async (req, res) => {
try {
if (req.files.video !== undefined) {
const video = req.files.video // this is your file do what ever you want with it
const videoname = Date.now() + req.files.video.name + ".webm"
video.mv(`${__dirname}/../your_path/${videoname}`, err => {
if (err) {
console.log(err)
return res.json({ err })
}
})
}
res.json(req.body)
} catch (err) {
res.json({ success: false, err: err, msg: "Server error" })
console.log(err)
}
})
Using express-fileupload to upload a file you can do it with your favourite one.
Related
I'm trying to send a file from my NodeJS Lambda function to Dailymotion, and I'm using the following code:
// Download the original file to the `tmp` folder of Lambda.
await axios.get('https://cdn.mysite.com/source.mp4', {
responseType: 'stream'
})
.then( response => {
response.data.pipe(fs.createWriteStream('/tmp/video.mp4'));
})
.catch(error => {
console.log(error);
});
const form = new FormData();
form.append('file', fs.createReadStream('/tmp/video.mp4'));
// Post the file to Dailymotion API.
axios.post('https://upload-xx.xxx.dailymotion.com/upload?uuid=xxxxx&seal=xxxxx&extra=xxxxx', form, {
headers: {
...form.getHeaders,
'Content-Type': 'multipart/formdata',
'Content-Length': fs.statSync('/tmp/video.mp4').size
},
})
.then(response => {
console.log(response);
})
.catch(error => {
console.log(error);
});
I can use the following URL to check the upload progress of the file: https://upload-xx.xxx.dailymotion.com/progress?uuid=xxxxx, but it seems that it uploads only the first chunk of the file and then stops, and I'm not getting any error or response.
Did I miss anything here?
When you use await, the result in your case is not a promise but stream. So there's no sense in adding old-style .then and .catch to not-promise essense.
Try the following.
try {
const stream = await axios.get('https://cdn.mysite.com/source.mp4', {
responseType: 'stream'
});
stream.pipe(fs.createWriteStream('/tmp/video.mp4'));
stream.on('error', (err) => console.log({ err }));
stream.on('close', async () => {
try {
const form = new FormData();
form.append('file', fs.createReadStream('/tmp/video.mp4'));
// Post the file to Dailymotion API.
const postThis = await axios.post('https://upload-xx.xxx.dailymotion.com/upload?uuid=xxxxx&seal=xxxxx&extra=xxxxx', form, {
headers: {
...form.getHeaders,
'Content-Type': 'multipart/formdata',
'Content-Length': fs.statSync('/tmp/video.mp4').size
},
});
console.log({ postThis })
} catch (err) { console.log({ err }) }
})
} catch (err) { console.log({ err }) }
How to fix this error message?
[xhr.js?b50d:178 POST http://localhost:3000/editor/add net::ERR_CONNECTION_RESET][1]
It works and append data, but I get this error message...
My API looks like this:
app.js
app.post('/editor/add', function (req, res) {
let articleData;
let textData;
let article = {
title: req.body.title,
content: req.body.content
}
fs.readFile(urlPath, 'utf8', (err, data) => {
if (err) {
console.log('readfile => ' + err);
} else {
articleData = JSON.parse(data);
articleData[article.title] = article.content;
textData = JSON.stringify(articleData, null, 2);
fs.writeFile(urlPath, textData, 'utf8', (err) => {
if (err) {
console.log('write file => ' + err);
} else {
console.log('Finished writing');
}
});
}
});
});
And my Axios POST method looks like this.
editor.vue
submitEditor: function() {
var self = this;
self.$axios({
headers: {
"Content-Type": "application/json"
},
method: "post",
url: "http://localhost:3000/editor/add",
data: {
title: "test5",
content: self.html
}
})
.then(res => {
console.log(res);
})
.catch(error => {
if (!error.response) {
// network error
this.errorStatus = "Error: Network Error";
} else {
this.errorStatus = error.response.data.message;
}
});
}
I use Vue/cli, I separate my client code and my server code. They are on a separate folder. I put Vue/cli in my client folder, and express.js in my server folder.
Thank you in advance!
Try sending a response from your route:
fs.writeFile(urlPath, textData, 'utf8', (err) => {
if (err) {
console.log('write file => ' + err);
} else {
console.log('Finished writing');
res.json({ msg: 'success' }); // send the client something
}
});
this is how I try it
await this.base64.encodeFile(uri).then((base64File: string) => {
console.log(base64File);
base64_data = base64File
alert(JSON.stringify(base64_data));
console.log(base64_data);
}, (err) => {
console.log(err);
alert(JSON.stringify(err));
});
this.db.putAttachment(att_id,att_name,base64_data, att_file_type).then(function (result) {
console.log(result);
alert(JSON.stringify(result));
}).catch(function (err) {
// ouch, an error
console.log(err);
alert(JSON.stringify(err));
});
it works on image file but it return "" for pdf files
you can convert file data to binary data or base64
this example convert to binary data
first download file with XHR request can use jquery ajax , axios or ...
set responseType: 'arraybuffer'
after download or before download create document in pouch db
axios.get(url, {
progress: false, responseType: 'arraybuffer',
onDownloadProgress: (progressEvent) => {
precent = (100 * progressEvent.loaded / progressEvent.total)
console.log(precent)
}
})
.then(resp => {
//get db
let db = $db.dbModel
//set attach
db.get(doc._id).then((doc) => {
db.putAttachment(doc._id, 'index.mp4', doc._rev, new Blob([new Uint8Array(resp.data)], {type: 'video/mp4'}), 'video/mp4')
.then(res => {
// console.log('success store file')
})
})
})
https://github.com/mohammadnazari110/pwa_offline_video_download
I have this so far:
app.get('/:image/:size.:ext', (req, res) => {
const remote = bucket.file(`${req.params.image}/${req.params.size}.${req.params.ext}`)
if (req.query.download == 'true') {
res.set('Content-Type', 'image/jpg')
res.set('Content-Disposition', `attachment; filename="${req.params.image}.${req.params.size}.${req.params.ext}";` )
res.set('Content-Transfer-Encoding', 'binary')
// header("Content-Length: ".filesize($filename))
}
// if (isProd)
// res.set('Cache-Control', 'public, max-age=604800')
remote.createReadStream({ validation: false })
.on('error', error => {
console.log(error)
res.send(`data:image/gif;base64,R0lGODlhAQABAIAAAAAAAP///yH5BAEAAAAALAAAAAABAAEAAAIBRAA7`)
})
.pipe(res)
})
In that download=true block, I am not sure how to get the filesize, if it is somehow passed in through remote stream, or if I should be storing that in a database somewhere, or otherwise get is somehow.
You can get metadata.size from
getMetadata(options, callback) that returns Promise containing GetFileMetadataResponse
storage
.bucket(bucketName)
.file(filename)
.getMetadata()
.then(results => {
const metadata = results[0];
console.log(`File: ${metadata.name}`);
console.log(`Size: ${metadata.size}`);
})
.catch(err => {
console.error('ERROR:', err);
});
how can i send a file(docx) to a user ?
this is my server code :
app.get('/api/topic/file/:id', function (req, res, next) {
Topic.findByIdAndUpdate(req.params.id)
.exec()
.then((topic) => {
let filepath = topic.news_file[0]
console.log('filepath', filepath)
res.download(filepath, topic.name + '.docx', function (err) {
if (err) {
console.log('api get file err ', err);
} else {
// decrement a download credit, etc.
}
});
}).catch((err) => console.log('error', err));
})
this does not trigger a download on the browser.
i am using react as front-end.
on the client i have a button triggering this upon click :
handleDownload() {
if (this.state.lastClicked) {
fetch("/api/topic/file/" + this.state.lastClicked._id)
.then(results => {
console.log('results', results)
return results;
})
} else {
//somthings...
}
}
Found a solution using downloadjs..
var download = require("downloadjs")
async handleDownload() {
const res = await fetch("/api/topic/file/" + this.state.lastClicked._id);
const blob = res.blob();
download(blob, this.state.lastClicked.name + '.docx');
}