Can I send XLS/XLSX files using Express res.sendFile()? - express

I've posted too many questions about this but I keep hitting a wall so I keep asking. Can you send an XLS or XLSX file using Express' res.sendFile()?
Here's the code I'm using:
res.sendFile(
path.join(__dirname, '../testing/'),
`${fileName}`,
(err, data) => {
if (err) throw err;
}
);
Even though fileName has the value of filename.xlsx, it never uses the XLSX file; it always defaults to index.html. I always get the following error:
[Error: ENOENT: no such file or directory, stat '/Users/username/Documents/app/testing/index.html']

sendFile neither knows nor cares that you're trying to send an XLS/XLSX file. It only knows that you're trying to send a file.
So, yes, you can, but you have to call sendFile properly. 😜
You've put the filename as a separate argument (which will be parsed as the options argument), instead of putting it in the path argument.
res.sendFile(
path.join(__dirname, '..', 'testing', fileName),
(err, data) => {
if (err) throw err;
}
);
Notice that I took the liberty of removing the apparently redundant expression template literal stuff. I've also split '../testing/' up for you: you're supposed to put discrete components into path.join as much as possible, for clarity.

Related

get file type after downloading from IPFS

I'm using ipfs to download files... but ipfs does not have filenames or extensions.
How can I properly set the extension for the saved file, based on the downloaded data?
You can add files with ipfs add -w to "wrap" them in a directory. This will preserve the filename.
There are also plans to record metadata like mime types along with files but, unfortunately, progress has gotten a bit stuck on this front.
There is no way to conclusively get the file extension in IPFS, so instead, you can look at the contents of the file to infer its file type (using file-type, for example)
When you have the full contents of the file in a buffer, you can do:
import {fileTypeFromBuffer} from 'file-type';
let buffer = /* buffer of file from IPFS */;
// undefined or string of file extension
let ext = (await fileTypeFromBuffer(buffer))?.ext;
If you wan't to do this in a React app on the frontend this works :
let contentType = await fetch(imageUrl)
.then(response => {
return response.blob().then(blob => {
return {
contentType: response.headers.get("Content-Type"),
raw: blob
}
})
})
.then(data => {
return data.contentType
})
file-type is intended for use in Node apps, not on the client side.
If you try to use a library intended for Node then you will get errors that relate to the library's internal dependencies on built-in Node modules which are not available in the browser. You should use a different library for making HTTP requests from the browser; I'd suggest using the browser's built-in fetch.
you must get the program file type, then import the extension name on the program name you downloaded from the buffer
i hope you understand this

How can I ensure this vue application doesn't exceed the recommended 244kb in production?

There is a vue file here that imports a json file that has about 9000 records in it.
How do I ensure that the json file is not compiled with the component?
A simple way would be to put the JSON file you want to access in the public folder (Webpack won't process anything in this folder). Then use AJAX to call the file at run time.
This will avoid expanding your app bundle, but Vue may still show that you're including a large resource. This approach would also allow you to split the data into smaller chunks and load them as needed in your app.
Here's an example, assuming the data file is located at /public/data/mydata.json. Also, I suggest using Axios to make the AJAX stuff easier.
import axios from 'axios'
export default {
name: 'Vue Component',
created() {
this.fetchData();
},
methods: {
fetchData() {
axios.get('/data/mydata.json').then(response => {
// do something with response.data
})
}
}
}
Use dynamic import. Like this:
import(
/* webpackChunkName: "my_json" */
'./src/my.json'
).then(({default: myJson}) => {
// do whatever you like here~
console.log(myJson);
});
doc:
https://webpack.js.org/guides/code-splitting/#dynamic-imports
If the json file is too big, you will still get the size exceeding warning.
But the json file would load async, so it would not cause any performance problem.
🔽 🔽 🔽 if you really don't want to see the warning, try this: 🔽 🔽 🔽
Use copy-webpack-plugin,it can copy your json file to dist folder, which means you can fire a XHR get request to load the json file, like this axios.get('/my.json').
By doing this, you can get the FULL control about when to load the file.

Storage.list() returning an empty array

I am trying to make a webpage to view all of the files in a Cognito accounts private folder in S3. I'm using the Amplify Auth and Storage plugins, and I know they are both configured properly because I already have file upload working. I found this thread on Github issues, but none of what is mentioned in that thread helped me. With the debug logs, I can see that it is properly resolving the path, as I am getting the correct path shown, but only an empty array is being returned. I've even tried resolving the public folder, which has a test file in it just to make sure its not an access thing, but that has the exact same issue.
This is the correct path its looking up, with xxxx replacing the account ID.
[DEBUG] 58:16.450 AWSS3Provider - list * from downloads/us-east-1:xxxxxxxx-xxxx-xxxx-xxxx-xxxxxxxxxxxx/*
const downloadPrefix = {
private: 'downloads/',
};
Auth.currentCredentials();
Storage.list('*', {
level: 'private',
customPrefix: downloadPrefix
})
.then(result => {
this.message = result.length
})
.catch(err => {
this.message = err
}
);
Thanks in advance for any help you may be able to provide! I've been stuck on this all day.
I figured it out. Removing the * in the search term made it actually return results, which is not at all what I would have expected, and should have tried to begin with. Storage.list('', ....) should fix your problem if you're doing the same thing as I am.

How to delete uploaded file in Sailsjs (req.file)?

In Sails.js, one could receive an uploaded file as such:
myControllerAction: function(req, res) {
req.file('avatar', function(err, uploadedFiles) {
// uploaded avatar image will be available here
console.log(uploadedFiles[0]);
}
}
Suppose I received a file, but it is not properly formatted the way I want. I would just reply with an error. One thing I would like to do is make sure that received file does not remain in the filesystem (i.e. if it exists somewhere, delete it). How can I ensure that?
Just use node fs module to delete uploaded file.
const fs = require('fs');
fs.unlink(insertFilePathHere, function(err) {
if (err) return console.log(err); // handle error as you wish
// file deleted... continue your logic
});

How do you specify file upload directory for express within sails?

I'm uploading a file from a browser <input type="file" name="myfile" id="myfile" using sails. I need to place it in other than the default express location. I'd use the following code in naked Express:
app.use(express.bodyParser({ keepExtensions: true, uploadDir: uploadFolder }))
For sails I wrote code along the lines of this relevant answer
First, I tried SkyTecLabs' answer. policies/configfileupload.js contains
'use strict';
var sailsExpress = require('../../node_modules/sails/node_modules/express'),
path = require('path');
console.log('.. initializing policies/configFileUpload');
module.exports = function configFileUpload (req, res, next) {
var uploadFolder = path.normalize(__dirname + '/../public/uploads');
console.log('.. in policies/configfileupload.js. uploadFolder=', uploadFolder);
console.log('typeofs are=',typeof req, typeof res, typeof next, typeof sailsExpress);
sailsExpress.bodyParser({ keepExtensions: true, uploadDir: uploadFolder });
next();
};
config/policies.js contains
'SchedController' : {
'uploadsubmit': 'configfileupload'
}
Express continues to upload the file to the default directory. Typeof req, res, next, sailsexpress are: object, object, function, function so the signature looks OK. (I tried returning the function configFileUpload just in case, but the controller was never called.)
Then I tried mikemcneil's suggestion. config/express.js
'use strict';
var sailsExpress = require('../node_modules/sails/node_modules/express'),
path = require('path');
module.exports.express = {
customMiddleware: function (app) {
var uploadFolder = path.normalize(__dirname + '/../public/uploads');
console.log('.. in config/express.js. uploadFolder=', uploadFolder);
console.log('typeof sailsExpress=', typeof sailsExpress, 'typeof app=', typeof app);
app.use(sailsExpress.bodyParser({ keepExtensions: true, uploadDir: uploadFolder }));
}
};
and the upload was still placed in the default directory. The typeofs sailsexpress and app are both function.
If the above code is correct for including express middleware in sails, then perhaps this middleware is performed only after express' formidable dependency parses the data and uploads the file.
I don't know in this case of any place in sails where I can place express configuration options such as bodyParser.
Warning: Before you release your app...
If you're expecting lots of file uploads, and especially big ones, you need to reconsider your strategy before deploying into production. You can replace the bodyParser middleware manually by replacing sails.config.express.bodyParser.
The default bodyParser in Express/Connect (and Sails, as of v0.9.x) uses a tmp directory to buffer files (similar to what you'd see in PHP). To get uploaded files somewhere else, you'll need to move them. Check out http://howtonode.org/really-simple-file-uploads for more on how to do that.
There is another option you might check out, depending on how adventurous you are. As I have time, I've been working on an alternative default bodyParser that can be used with Sails or Express. It defers to the underlying library for param/JSON parsing, but uses formidable's raw onPart events to allow streaming uploads without writing the entire file to disk. More on that:
Example repo: https://github.com/mikermcneil/stream-debug
Discussion: https://groups.google.com/d/msg/sailsjs/525fK7pgK8U/bduPudCSLUgJ
Source: https://github.com/mikermcneil/file-parser