I'm trying to insert a binary attachment to CouchDB with nano. I have a JPG in data returned by http.request.
I save it with nano as follows
db.attachment.insert( id, 'content', self._data, contentType, {rev: rev}, function(err, body) {
callback();
});
but when I try view it though a web browser the image is broken.
The file is full of UTF-8 escape characters which is visible when I pull it with CURL:
$ curl http://127.0.0.1:5984/web-crawler/doc-test.jpg/content
"ÿØÿà\u0000\u0010JFIF\u0000\u0001\u0001\u0001\u0000H\u0000H\u0000\u0000ÿâ\fXICC_PROFILE\u0000\u0001\u0001\u0000\u0000\fHLino\u0002\u0010\u0000\u0000mntrRGB XYZ \u0007Î\u0000\u0002\u0000\t\u0000\u0006\u00001\u0000\u0000acspMSFT\u0000\u0000\u0000\u0000IEC sRGB\u0000\u0000\u0000\u0000\u0000\u0000\u0000\u0000\u0000\u0000\u0000\u0000\u0000\u0000öÖ\u0000\u0001\u0000\u0000\u0000\u0000Ó-HP \u0000\u0000\u0000\u0000\u0000\u0000\u0000\u0000\u0000\u0000\u0000\u0000\
Content is not corrupted because if I save it to a file I can see the image.
var fs = require('fs');
fs.writeFile('logo.jpg', data, 'binary', function(err){
if (err) throw err
console.log('File saved.')
});
What is the right way to do it?
Ok, this question can be close. The answer to my problem is Buffer:
db.attachment.insert( id, 'content', new Buffer(self._data, "binary"), contentType, {rev: rev}, function(err, body) {
callback();
});
Related
When uploading my file to s3, I upload it publicly, but when accessing it through its URL, the file is downloaded automatically, what I want to achieve is to be able to view it in any browser without having to download it.
I upload it through node.js, I attach the code
fs.readFile(dataString, (err, data) => {
if (err) throw err;
var parametrosPutObject = {
Bucket: bucket,
Key: dataString,
Body: data,
};
s3.putObject(parametrosPutObject, (err, data) => {
if (err) throw err;
console.log("recorded file");
//obtenerURL();
});
});
You will need to provide a Content-Type header for the file that browsers interpret as viewable-in-browser.
If the object is e.g. a JPEG file, providing Content-Type: image/jpeg should clue browsers in that they don't need to download the file.
I've been stuck on this problem for awhile now and can't seem to figure out why this file isn't being uploaded to firebase correctly. I'm using the code below in firebase functions to generate a document, then I convert that document to a stream, finally I create a write stream declaring the path that I want the file to be written to and pipe my document stream to the firebase storage WriteStream.
Example 1: PDF uploaded from file system through firebase console. (Link works and displays pdf)
Example 2: PDF generated in firebase functions and written to storage using code below
Considerations:
I know the PDF is valid because I can return it from the function and see it in my web browser and everything is as I would expect.
When trying to open the bad file it doesn't display anything and redirects me back to the overview screen.
const pdfGen = require("html-pdf");
pdfGen.create(html, { format: "Letter" }).toStream(function (err, stream) {
if (err) return res.status(500).send(err);
var file = storage.bucket()
.file(job.jobNum + "/quote-doc.pdf")
.createWriteStream({
resumable : false,
validation : false,
contentType: "auto",
metadata : {
'Cache-Control': 'public, max-age=31536000'}
});
stream.pipe(file)
.on('error', function(err) {
return res.status(500).send(err);
})
.on('finish', function(data) {
stream.close();
file.end();
res.end();
console.log('finished upload');
});
});
Hi i'm trying to download a pdf with nodejs (UNIREST) but i'm a bit stocked.
var url_pdf = "http://myurl.com/ex.pdf"
unirest.get(url_pdf)
.as.binary(function (response) {
fs.writeFile("./pdf/test.pdf", response.body, 'binary', function (err){
if (err)
console.log(err);
});
})
When wrting the file, its miss formatted (just a blank pdf file) size is ok.
I want get content from my uploaded file. Uploaded file is just text file and i want read this file line by line
req.file("model").upload(function (err, uploadedFiles){
if (err) return res.serverError(err);
return res.json({
message: uploadedFiles.length + ' file(s) uploaded successfully!',
files: uploadedFiles,
content: uploadedFiles[0] // I want to get my uploaded file content
// Stream or buffer
});
});
You can get the file descriptor from uploadedFiles[0].fd
Use it to read / stream the file.
fs.readFile(uploadedFiles[0].fd, 'utf8', function (err,data) {
return res.json(200, {message: 'Ok', data: data});
})
First install Local filesystem streaming binary adapter for Sails.js / Waterline
npm install sails-local-fs
in your code create fs instance
var fs = require('fs');
And now use readFile() method to get content of your file
fs.readFile(files[0].fd, 'utf8', function (err,data) {
return res.json(200, {message: 'Ok', data: data});
})
I am trying to fetch the image remote then process it by node-tesseract. Code following:
var request = require('request');
var fs = require('fs');
request.get('http://cn.bing.com/s/a/hpc18.png').pipe(fs.createWriteStream('bing.png'));
Code above is doing well and the png file will be saved correctly.
Then I want to process the png by tesseract ocr(node binding)
tesseract.process('bing.png', options, function (err, text) {
//do something
});
After running all code above, I found that the text is null. Then i checked the picture, the png file didn't generate correctly - it's an empty file.
Anyone could help? I tried to sleep some time between those two parts but it didn't work. Why even the png file wasn't generated?
Regards,
Lyu
Can you try this out:
var fs = require('fs');
var request = require('request');
var tesseract = require('tesseract');
request.get({url: 'http://cn.bing.com/s/a/hpc18.png', encoding: 'binary'}, function (err, response, body) {
fs.writeFile("bing.png", body, 'binary', function(err) {
if(err)
console.log(err);
else
tesseract.process('bing.png', options, function (err, text) {
//do something
});
});
});