I am trying to read text file from s3 storage in ReactJS. I have the s3 bucket link of the text file. However, I am not able to read the text file from the link. I have googled a lot about this, but everywhere people are reading the file locally and not from a link.
The link of the file looks like this:
https://bucketnmame.s3.amazonaws.com/folder1/folder2/file.txt
This link is being stored in database with NodeJS as backend. Would be great if someone helps me...
Try this
fetch('https://bucketnmame.s3.amazonaws.com/folder1/folder2/file.txt')
.then ((response) => response.text())
.then (data => {
console.log(data)
});
Related
I'm developing a Node.js Server. This Server makes Calls to an API. You can send Files to the Node.js Server and that Server will send those File to another API that will process the File. When the API processed the File, the Node.js Server will download the File and send it to the Requestor. This Requestor can download the File.
Now when I try to make the request with Microsoft Office Files like .docx or .pptx, they corrupt. However it works perfectly fine with a .txt File for example.
The Data I send back to the Client looks something like this:
[Content_Types].xml��MN�0►��↕y�↕7,►BM��P�^��'���dO��Ƃ#q♣&)♦�*�R�������y{y↔O��dk�I{W��↑�♀��J�E�V���lR�g�)�V�*�D♀7�'�♦+R�♥8�4>Z�t�♂▲�|▬♂����§��!8̱�`��♫→�2��o��▲K�,�������►��☻�
��N���i�♦����H☺��☻��H�◄5��N�▼�E0�8�G��&�����♂�H��ZA6§◄▼�%=Nަd&Q��☺?����T��▼§wr�N[5☻KH��♣k��b�v�F60⌂☻D�M������t6�▲pg�∟♫:�a>�]���,˓}tB�317⌂�C�{��♣�~R�;PK☺☻¶�n&V,&3�u☺0♥►docProps/app.xmlPK☺☻¶�n&V��i��☺�☻◄�☺docProps/core.xmlPK☺☻¶�n&V⯨��☻)♂◄\♥word/document.xml
PK☺☻¶�n&V�
!♦�♂◄�word/settings.xmlPK☺☻¶�n&V�♠�Ys♂↔s☼8♀word/styles.xmlPK☺☻¶�n&V��m��♠� ↨�↨word/theme/theme111.xmlPK☺☻¶�n&V�/�⌂�☺z♠↕�▲word/fontTable.xmlPK☺☻¶♥�n&Vψ��D☻D☻♂� _rels/.relsPK☺☻¶�n&V��V=�F♥∟/#word/_rels/document.xml.relsPK☺☻¶�n&V�l8=C☺�♦‼R$[Content_Types].xmlPK♣♠♂♂�☻�%
The Error of Word looks like this:
enter image description here
Does anyone know what I'm doing wrong.
(It's the first Node.js Server i'm developing btw, so please don't be too harsh with me)
Here's what I have so far:
app.post("/document", upload.single('file'), async (req, res) => {
// Code to upload the Document to the API
await axios.post(
`${API_BASE_URL}/document/${id}/result?auth_key=${API_KEY_VALUE}&document_key=${key}`
).then(( data ) => {
console.log("return data");
console.log(data.headers);
res.setHeader('Content-Type', data.headers['content-type']);
res.setHeader('Content-Disposition', data.headers['content-disposition']);
res.setHeader('Content-Length', data.headers['content-length']);
res.setHeader('Connection', data.headers['connection']);
res.setHeader('Date', data.headers['date']);
res.setHeader('x-trace-id', data.headers['x-trace-id']);
res.setHeader('strict-transport-security', data.headers['strict-transport-security']);
res.setHeader('access-control-allow-origin', data.headers['access-control-allow-origin']);
res.send(data.data);
})
})
I can always download the Document in the right File Type etc. but when I try to open it, it says something like "can't read the File":
I am trying to get a list of files(pdf) or at least their names from an directory. With pure js it seams simple with require('fs') and that stuff. But on my Webserver its not that simple.
I hope sofern can help me
Due to security reasons, it‘s not directly possible to read the folder structure of servers. You could create a JSON file where a list of all PDF files lays and then fetch() this file from the website and display a list. You can assemble links from these names by adding something like
https://server.example/files/
to the beginning of the filenames.
How to use fetch():
fetch('your url')
.then(response => response.json())
.then(data => console.log(data));
I wish you much luck 🍀
I am currently using RecorderJS and need to send a .wav file to the backend. the API is quite limited in documentation so I am struggling to figure out how to send the .wav file through my axios.post(...).
I am able to download the .wav file with
Recorder.download(theblob, 'audio.wav');
this downloads a .wav file which I can play through itunes so it is the right format. I now need to figure out how to save this in a variable in order to post it through axios. Also, what should i use for me headers, .. ect?
Looking for any kind of javascript solution to this. I just need to send the exact downloaded file to my backend. Thanks!
The download method stores the file somewhere on your disk. I believe javascript cannot traverse your computer's path and read files for security reasons. I'm not sure if recorder-js offers storing it in a variable "out of the box", so you may want to get that handled first.
For your second part of the question:
This should work for posting it to the back-end:
let data = new FormData();
data.append('wavfile', file, file.name);
const config = {
headers: { 'content-type': 'multipart/form-data' }
}
axios.post('/api/recorderfiles', data, config)
after looking on the Discord.js docs i can find an answer for the question, does someone know how to do it? theres already a question the page but has no answers or comments.
imagine that someone on the chat sentí an image, is there a way of the bot downloading the image or get the url of the image?
thanks!
For starters... You'd need the code to access the attachment.
client.on(`message`,function(msg){
if(msg.attachments.first()){//checks if an attachment is sent
if(msg.attachments.first().filename === `png`){//Download only png (customize this)
download(msg.attachments.first().url);//Function I will show later
}
}
});
Note: I limited attachments to png only so we download verified images. Otherwise we might download some bad scripts and possibly viruses. Be careful when downloading stuff.
Now the code I just gave you calls download and passes in the url.
Now you will need the request module AND the fs module.
Why? Glad you asked... The request module accesses the url and pulls it the data from the web.
The fs module create/reads/writes files on your local/external machine...
Using the two modules, we will pull it and then save it.
Now lets assume url is this meme.png (discord png attachment)
let request = require(`request`);
let fs = require(`fs`);
function download(url){
request.get(url)
.on('error', console.error)
.pipe(fs.createWriteStream('meme.png'));
}
and Voila! We now have a meme.png image about Doritos XD
Besides the link I posted in the comments section, from looking at the Discord documentation, it appears the url is in the attachment object.
I would like to get information on file, at least only the information on number of pages from nodejs in client side (react). I was able to get the same for PDF files using PDFJs. Could someone point as to how it can be done for other file types like word, xls and ppt ? If there are external APIs which would provide this service, pointing that would be helpful too.
For getting page count in docx and pdf files you can use https://www.npmjs.com/package/docx-pdf-pagecount
const getPageCount = require('docx-pdf-pagecount');
getPageCount('E:/sample/document/aa/test.docx')
.then(pages => {
console.log(pages);
})
.catch((err) => {
console.log(err);
});
getPageCount('E:/sample/document/vb.pdf')
.then(pages => {
console.log(pages);
})
.catch((err) => {
console.log(err);
});
You can use XLSX to parse spreadsheet-like files. XLSX can parse the files and return all the info of them.
But you can only retrieve the meta info until you use XLSX to parse those files. That means, no matter what, you have to parse them. If your files are big, it would be a performance issue for client browsers if you do it on client slide.
Update:
A hint, you can find some tools to detect the file type of the files, and deliver them to the corresponding parser the get the meta info.
For now, there is no such library implemented natively in JavaScript. If you are fine with some other none-pure node modules.
Like textract, see how it works.