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":
Related
I need to send the generated PDF as a whatsapp message, but it shows an error when I try to read my file.
easyinvoice.createInvoice(data, function(result) {
//The response will contain a base64 encoded PDF file
fs.writeFileSync("invoice.pdf", result.pdf, 'base64');
var s = result.pdf;
client.messages
.create({
mediaUrl: s,
body: 'invoice',
from: 'whatsapp:+somenumber',
to: 'whatsapp:+somenumber'
})
.then(message => console.log(message.sid))
.done();
});
Midlaj, hi! Twilio developer evangelist here.
You cannot use a local path because media url has to be publicly accessible to Twilio's servers (ie it has to have a URL) so that the asset can be retrieved and added to the message. If a local path were accepted, it would point to "/Users/Name/file.gif" on a random Twilio server.
You can use Twilio Assets to send local files. You could alternatively also use Ngrok.com to expose your local webserver to the internet, hosting the files with ngrok and sharing the local files externally.
Let me know if this helps at all!
I have a csv file and I want to open it (in excel) with plain javascript.
I've searched a lot of websites and none of them seem to have the answer.
ANSWER
const { exec } = require('child_process');
exec('start ./csv/Fixture.csv', (err, stdout, stderr) => {});
You will need to use node.js or some sort of server to deliver the payload or the csv file to a JavaScript buffer such as via a websocket. Client side browser JS is a sandbox and you need to get your data from the server, or use node.js server to write javascript that runs on the server and can access files, but I think you mean you want to access a csv file on a webpage, and to do that you would need to have server side code send it to you, by using a XHR or WebSocket transfer.
Use the new HTML anchor tag attribute, for example:
<a href="abc.csv" download> Click Here... </a>
I've got a big system where I need to generate a PDF file. I'd like to access it via REST API and, of course, store the file locally.
The file content depends on many parameters what the content of this file should be, i.e. time: from-to, filters, sorting and many, many more parameters; they form a JSON object which perfectly fits into POST parameters. The parameters cannot go through GET, since they're too big.
There is the FileSaver library that works perfectly fine on modern browsers. I created an online demo. But when I downloaded old browsers - firefox11, firefox12, firefox15, it didn't work at all, even though I included the Blob.js polyfill - it opened a new tab with URL like: blob:457-343457-34574567-4576456 that was unable to be saved. I need to support many browsers, not only the new ones.
The question is - I've got JSON parameters object inside my SPA app - how should I design this PDF binary file download?
I was thinking of 3 approaches:
force browser to create a file on localhost - using FileSaver. WOrks fine for modern browsers, doesn't work for old ones
create downloadable link. I shoot a POST to the REST API, incuding all parameters, the REST API returns something like: {"download": "mysite.com/download/ms2h5d34h53m"}, the response is used to display a link to the user; the user might click the link (with no AJAX) and the server-side API should just return a file like in the old times.
not mine, but somewhere I read I could create an invisible form that shoots a POST to the server, which triggers file download (perhaps this would reduce the step with returning the {"download": "mysite.com/download/ms2h5d34h53m"} JSON)
I need a guidance on how to do that right.
I tried to create a test express.js server below. When I access http://localhost:8081/download directly, I see a PDF file downloaded locally. But when I try to access it via ajax/js:
then the content is fetched as binary stream:
var fs = require('fs');
var express = require('express');
var app = express();
app.get('/', function (req, res) {
res.send('Hello World');
});
app.get('/download', function(req, res){
var file = fs.readFileSync(__dirname + '/example.pdf', 'binary');
res.setHeader('Content-Type', 'application/pdf');
res.setHeader('Content-Length', file.length);
res.setHeader('Content-Disposition', 'attachment; filename=new-name.pdf');
res.setHeader('filename', 'sample.pdf');
res.write(file, 'binary');
res.end();
});
var server = app.listen(8081, function () {
var host = server.address().address;
var port = server.address().port;
console.log("Example app listening at http://%s:%s", host, port);
});
I have created a Node.js server that worked well when the Javascript was part of the HTML page. I moved the JS to another file and added some images. Now it won't load the images or the JS into the browser. However, the web page renders perfectly when I open the web page directly. This is what my server looks like:
app.get('/',function(req,res){
res.end('Hello World!Go to /map to see the google map');
});
app.get('/map',function(req,res){
var conn;
//images must be sent to the client from the server...
res.sendfile(__dirname+'/client/google_maps.html');
//receiving requests from jQuery
});
I am not using the Express project structure or the Express middleware or Express configuration to do this.
If that's all your code, I think the problem you met is reasonable. You didn't tell your server how to respond your images and scripts when browser requested. For example in your google_map.html file you have <script src="myjs.js"></script>, then your browser will ask your node application to give the content of myjs.js but your server don't know how to deal with it.
You could try to add code like below to see if it helps.
app.get('/myjs.js', function (req, res) {
res.sendfile(__dirname + '/myjs.js');
});
As dimadima said, Express provides a module to handle static files that you can use like
app.use(express.static(__dirname + '/public'));
I have a download button in HTML that when is clicked a POST request is made via ajax sending the filename of the file that must be downloaded.
On the server side I do something like this:
function download (req, res) {
...
// path is an absolute path to a file that is not in the public
// directory. I want to download that file
res.writeHead(200, {
"Content-disposition": "attachment;filename=\"" + path + "\"",
"Content-Type": "text/csv"
});
var filestream = fs.createReadStream(path);
filestream.pipe(res);
};
I can see the file content in the response but the save file dialog doesn't appear.
Which is the issue? How can I fix this?
I use only built-in node modules, so I don't use express.
You can't force a browser to show a Save File dialog.
With your example, on my Mac both Chrome and Safari download the file automatically (in Chrome it's a setting whether or not to show a dialog which – I think – is turned off by default; I don't know if Safari has a similar setting), whereas FireFox does give me a dialog.