Quick question from a NodeJs beginner:
I'm using express-formidable in order to upload files to my server. How do I save them? n_n'
Inspecting the variable that holds the file I have:
But I don't know what to do with it. I have checked some methods from fs and tried writeFile but of course it only writes [Object object].
Thanks
the File here not a browser's File, just a common object, but looks like File.
for avoid out of memory, the file has already saved in a temporary folder
you can just do like this:
const {name, path} = req.files.input
fs.writeFileSync(name, fs.readFileSync(path))
Related
I'm currently using "require(./file.json)" because I can't get anything else to work.
So I want to be able to reload config file so I can disable / enable some commands in my bot without having to restart it.
Any Help or Suggestion will be appreciated!
If JSON doesn't work then any other file that can be used as a "config/db" is fine!
Thank you!
To read a JSON file, you need to use the built-in fs module.
const fs = require('fs');
const myFile = JSON.parse(fs.readFileSync('file.json'));
//edit myFile however you want
fs.writeFileSync('file.json', JSON.stringify(myFile, null, 4));
use fs to read file contents, instead of include or require, When you make include, it will read once in run-time only.
Also you can use require inside functions, which makes read in every function call. This is not pretty (In my opinion), But it works.
I am sending a gzip file from Postman to a Flask endpoint. I can take that binary file with request.data and read it, save it, upload it, etc.
My problem is that I can't take its name. How can I do that?
My gzip file is called "test_file.json.gz" and my file is called "test_file.json".
How can I take any of those names?
Edit:
I'm taking the stream data with io.BytesIO(), but this library doesn't contain a name attribute or something, although I can see the file name into the string if I just:
>>>print(request.data)
>>>b'\x1f\x8b\x08\x08\xca\xb1\xd3]\x00\x03test_file.json\x00\xab\xe6RPP\xcaN\xad4T\xb2RP*K\xcc)M5T\xe2\xaa\x05\x00\xc2\x8b\xb6;\x16\x00\x00\x00'
Further to the comment, I think the code which handles your upload is relevant here.
See this answer regarding request.data:
request.data Contains the incoming request data as string in case it came with a mimetype Flask does not handle.
The recommended way to handle file uploads in flask is to use:
file = request.files['file']
file is then of type: werkzeug.datastructures.FileStorage.
file.stream is the stream, which can be read with file.stream.read() or simply file.read()
file.filename is the filename as specified on the client.
file.save(path) a method which saves the file to disk. path should be a string like '/some/location/file.ext'
source
I have a problem with importing any file as a file instance from local folder.
Can you help me please?
I can't find anything in 'fs' or 'path', maby because I don't know what to see
I want to get File and pass it to my JS applicatin as a File instance.
Start by using a path relative to your script and the __dirname directive.
fs.readFileSync(__dirname + '/myfile.txt')
You may also wish to lose the / using path to make your code more portable.
const path = require('path')
fs.readFileSync(path.join(__dirname, 'myfile.txt')
In regards to format, the result will be Buffer. If you want a string,
fs.readFileSync(filename,'UTF-8')
If you want a blob, see stackoverflow.com/questions/14653349
I'm creating a thumbnail from the first page of a PDF with the Node gm module.
var fs = require('fs');
var gm = require('gm');
var writeStream = fs.createWriteStream("cover.jpg");
// Create JPG from page 0 of the PDF
gm("file.pdf[0]").setFormat("jpg").write(writeStream, function(error){
if (!error) {
console.log("Finished saving JPG");
}
});
There's two problems with the script.
It creates a file cover.jpg, but that file is empty (size 0) and can't be opened by any viewer.
It creates a file named [object Object] that is an image of the PDF's first page (this is what I want, but the wrong name).
Aside from doing some additional file system manipulation to rename the [object Object] file after generating it, is there something I can change in the way I am using gm and fs in this script to write the image directly to the cover.jpg file?
This question is similar to what I am asking, but there is no accepted working answer and I need to install yet another library to use it (undesirable).
write receives the file path as the first argument, not a write stream, therefore the method is converting the stream object into its string representation, that's why it saves a file named [object Object].
You can just use .write("cover.jpg"), or if you want to use a write stream, you may use .stream().pipe(writeStream).
Take a look at the stream examples of gm.
I am creating an application using Bottle framework. I need a feature to upload an Excel file.
I am using the following for file upload.
http://bottlepy.org/docs/dev/tutorial.html#post-form-data-and-file-uploads
On the server side I am getting the file data as binary content. I want to save it in a temporary folder as an Excel file.
I am new to Python and Bottle. Any help will be much appreciated.
Thanks
Chirdeep
Your request.files.data object contains the data about your excel file. So you only need to create a temporary folder and save it inside. This can be done using the tempfile module
f = tempfile.NamedTemporaryFile(delete=False, suffix=".xlsx")
f.write(request.files.data.file.read())
f.close()
I was not able to get simple file writing code like yours to work, So I used the tempfile module. Looking at your code, I would have assumed it would write to the directory where the python file is, if the code is working. Try using the code below, if you don't pass arguments to dir, it will create a file in the current directory.
def save_as_temp_file(data):
with tempfile.NamedTemporaryFile(dir=settings.TEMP_PATH,
delete=False,
suffix=".xlsx") as f:
f.write(data.file.read())
return f.name