FuseDev
FuseDev

Reputation: 1

My nodejs website can't upload file than 300MB

So today I made my small website uploader But I found a problem. be like my code can't be given files than 300MB+

Index.js

var http = require('http');
var formidable = require('formidable');
var fs = require('fs');

http.createServer(function (req, res) {
  if (req.url == '/fileupload') {
    var form = new formidable.IncomingForm();
    form.parse(req, function (err, fields, files) {
      var oldpath = files.filetoupload.filepath;
      var newpath = '/var/www/html/file/' + files.filetoupload.originalFilename;
      fs.rename(oldpath, newpath, function (err) {
        if (err) throw err;
        res.write(`File be upload done! this is your URL!\n`);
res.write(`http://exmaple/file/${files.filetoupload.originalFilename}`)
res.write('\nor need short domain?\n');
res.write(`http://exmaple/file/${files.filetoupload.originalFilename}`);
res.write('\nnow just copy link url and share!');
        res.end();
      });
 });
  } else {
    res.writeHead(200, {'Content-Type': 'text/html'});
    res.write('<form action="fileupload" method="post" enctype="multipart/form-data">');
    res.write('<input type="file" name="filetoupload"><br>');
res.write('click a submit to upload! --> ');
    res.write('<input type="submit">');
res.write('\n\n New Problem file size than 300MB website will wont upload! we will fix it to quciky!');
    res.write('</form>');
    return res.end();
  }
}).listen(8080);


What I did to fixed?

Upvotes: 0

Views: 175

Answers (1)

0x3CE
0x3CE

Reputation: 137

I was able to find a solution here: https://www.section.io/engineering-education/uploading-files-using-formidable-nodejs/

To summarize :

You need to create a variable pointing to the directory/folder where you want to store the files. To do this, add the following code after creating the form instance:

const uploadFolder = path.join(__dirname, "public", "files");

Now, your uploadFolder variable points to the folder under the public directory which is present at the root level of your project.

Now let’s change the configuration by altering few properties in the form instance:

// Basic Configuration
form.multiples = true;
form.maxFileSize = 50 * 1024 * 1024; // 5MB
form.uploadDir = uploadFolder;
console.log(form);

Normally, you change the maximum size and it should work

Upvotes: 1

Related Questions