GithubHelp home page GithubHelp logo

richardgirges / express-fileupload Goto Github PK

View Code? Open in Web Editor NEW
1.5K 1.5K 255.0 1.48 MB

Simple express file upload middleware that wraps around busboy

License: MIT License

JavaScript 99.74% HTML 0.26%
busboy connect express-fileupload express-middleware file-upload files fileupload forms middleware multipart nodejs upload

express-fileupload's People

Contributors

amazingmech2418 avatar closingin avatar code42cate avatar dependabot[bot] avatar duterte avatar jameshemery avatar jeanbmar avatar markxoe avatar michaelgrigoryan25 avatar mikeesto avatar mlinquan avatar nusu avatar pajtai avatar pawelsuwinski avatar piotr-skowronski-rmtcfm-com avatar pronein avatar r3wt avatar richardgirges avatar robintail avatar romanburunkov avatar shakmr avatar shudrum avatar somewind avatar targos avatar tomhigginsuom avatar tumulr avatar tycrek avatar vojtastanek avatar wbt avatar zwade avatar

Stargazers

 avatar  avatar  avatar  avatar  avatar  avatar  avatar  avatar  avatar  avatar  avatar  avatar  avatar  avatar  avatar  avatar  avatar  avatar  avatar  avatar  avatar  avatar  avatar  avatar  avatar  avatar  avatar  avatar  avatar  avatar  avatar  avatar  avatar  avatar  avatar  avatar  avatar  avatar  avatar  avatar  avatar  avatar  avatar  avatar  avatar  avatar  avatar  avatar  avatar  avatar  avatar  avatar  avatar  avatar  avatar  avatar  avatar  avatar  avatar  avatar  avatar  avatar  avatar  avatar  avatar  avatar  avatar  avatar  avatar  avatar  avatar  avatar  avatar  avatar  avatar  avatar  avatar  avatar  avatar  avatar  avatar  avatar  avatar  avatar  avatar  avatar  avatar  avatar  avatar  avatar  avatar  avatar  avatar  avatar  avatar  avatar  avatar  avatar  avatar  avatar

Watchers

 avatar  avatar  avatar  avatar  avatar  avatar  avatar  avatar  avatar  avatar  avatar  avatar  avatar  avatar  avatar  avatar  avatar  avatar  avatar  avatar  avatar  avatar  avatar

express-fileupload's Issues

Restify?

Will this work on restify? or this library is limited with express?

Upload Large file

Hello, I'm trying upload big file (40Mb) and get this error:
buffer.js:11
super(arg1, arg2, arg3);
^

RangeError: Array buffer allocation failed
at Buffer.Uint8Array (native)
at FastBuffer (buffer.js:11:5)
at createUnsafeBuffer (buffer.js:38:12)
at allocate (buffer.js:181:12)
at Function.Buffer.allocUnsafe (buffer.js:141:10)
at Function.Buffer.concat (buffer.js:309:23)
at FileStream. (/opt/pyxis_server/node_modules/express-fileupload/lib/index.js:27:24)
at emitOne (events.js:96:13)
at FileStream.emit (events.js:188:7)
at readableAddChunk (_stream_readable.js:176:18)

Uploading 10Mb file is ok.

<input ... multiple> doesn't work

In case you have a multiple file selection input, such as

<input type="file" name="uploadedFiles" multiple>

it won't work. Multiple files will be uploaded, but they will not appear in the files tree since this line

return req.files[fieldname] = {

(index.js line 53)
will be called one time per file, replacing the last file from the object.

I'll submit a patch in this Issue soon.

using fetch to upload file

How to use it with fetch api?
I wrote something like that, but on server I don't get anything.

const postFile = document.getElementById('post-file');
postFile.addEventListener('change', function(e) {
  const file = postFile.files[0];
  const reader = new FileReader();
  reader.readAsText(file);
  reader.addEventListener('loadend', (evt) => {
    fetch('/api/upload', {
      method: 'post',
      body: {
        input: evt.target.result
      },
      headers: {
        'Content-Type': 'application/json'
      }
    });
  });
});

Do not parse nested objects.

Request example


image

Expected Result

{  
   "name":"Yerevan Mall Food Court",
   "layout":{  
      "width":500,
      "height":400
   }
}

Actual Result


{  
   "name":"Yerevan Mall Food Court",
   "layout[height]":500,
   "layout[width]":400
}

limit fileSize

I'm not able to detect when a file has reached the limit specified by the options when the fileupload object is created.
The module runs the next() but without any message that the file have reached the max fileSize but the file is saved truncated.
Maybe a new field truncated can be added to req.file, indicating that the file has been truncated and it has exceeded the maxFileSIze.

Cannot read property 'mv' of undefined

Please what is the meaning of the mv in sampleFile,mv() and where does it come from? this is the error message I got when I run it: (Cannot read property 'mv' of undefined)

ES6 Promise version .vm

Trying to add a ES6 Promise version of the .mv function.

Here's the draft:

        mvPromise: function(path) {
          return new Promise((resolve, reject) => {
            let fstream = fs.createWriteStream(path);

            streamifier.createReadStream(buf).pipe(fstream);

            fstream.on('error', function(error) {
              reject(error);
            });

            fstream.on('close', function() {
              resolve();
            });
          });
        }

This should be appended after the .vm definition in let newFile.

Any suggestions? If this looks good, I'll create a pull request with a test.

Is it possible to restrict filetypes?

Currently working REST API for uploading the documents to user profile. Is there anyway to restrict the file types to upload. For example png, jpg, jpeg, pdf need to accept from client side?

.mv creates 0byte empty file

i got this to create files in my uploads directory but it isnt putting anything in the file. os.tmpdir says my temp dir is /tmp/ but there arent any uploads in there either.

Way to get path of the file?

Im using express-fileupload to get the file from the client side form but i need to use ftp to move it so i need the path, is there any way to do it using this?

Crashes when the multipart request is empty. And crashes the node instance.

Can be replicated by launching a rest client like Advanced REST client and trying to upload a file without selecting one.

6:57:30 PM web.1 | events.js:85
6:57:30 PM web.1 | throw er; // Unhandled 'error' event
6:57:30 PM web.1 | ^
6:57:30 PM web.1 | Error: Unexpected end of multipart data
6:57:30 PM web.1 | at /Users/xxxxxx/Documents/workspace2/node-tickets/node_modules/dicer/lib/Dicer.js:62:28
6:57:30 PM web.1 | at process._tickCallback (node.js:355:11)

Create md5 checksum on data

It might be an idea to create an md5 property.
Many uses really, but for me to ulpoad a file and have the md5 ready saves a few steps.

how to print the error when using .mv

I would like (now that everything is working perfectly) manage the errors.
I have the following code, which (purposely) does not work. i am trying to get the error message, but I never get the IAM HERE printed in the log, and of course no message is sent back.

SORRY - I don't know how to delete this issue. Brain meltdown due to the heat - everything works as expected

Tmp file?

I was wondering, if I don't use mv(), what happens to the file? Does it stay in a temporary folder?

express-fileupload

Hello
i have an issue with express-fileupload module when i upload the file , i receive a empty file with 0 octet !!

Multiple File Upload from Single Input

Hi, the documentation indicates express-fileupload supports multiple file upload when the files are coming from input tags with different names. But does the package support uploading multiple files from a single input tag with the multiple attribute? Something like this:
<input type="file" name="myPictures" multiple></input>

Getting "No 'Access-Control-Allow-Origin' header is present on the requested resource." error

XMLHttpRequest cannot load https://localhost:4000/upload-file. No 'Access-Control-Allow-Origin' header is present on the requested resource. Origin 'http://localhost:4200' is therefore not allowed access. The response had HTTP status code 500.

I have enabled on my Express server by doing :

const fileUpload = require('express-fileupload');
app.use(fileUpload());

app.use(function(req, res, next) {
    res.header("Access-Control-Allow-Origin", "*");
    res.header("Access-Control-Allow-Headers", "Origin, X-Requested-With, Content-Type, Accept, Cache-Control, Key, Access-Control-Allow-Origin");
    next();
});

But, all my other REST POST routes are working fine except the '/upload-file' route which uses your middleware. The request is not even reaching the controller.

And BTW the '/upload-file' route works perfectly fine when tested with POSTMAN. So there is no problem with logic

Nothing seems to work what do I need to do?

BTW I am using an angular-cli based angular js front end with Dropzone.js as a file upload plugin.

Uploaded file URL

Hello, Thanks for your simple file upload package!
I want to upload file and get URL of uploaded file as response.
Is there anyway to get this?
Thanks.

TypeError: callback is not a function

\node_modules\express-fileupload\lib\index.js:66
callback(null);
^

TypeError: callback is not a function
at WriteStream. (\node_modules\express-fileupload\lib\index.js:66:17)
at emitNone (events.js:91:20)
at WriteStream.emit (events.js:185:7)
at fs.js:2037:14
at \node_modules\graceful-fs\graceful-fs.js:43:10
at FSReqWrap.oncomplete (fs.js:123:15)
[nodemon] app crashed - waiting for file changes before starting...
Is it express-fileupload related?

My file is not `.mv` move to folder

Hello very nice work, i have a an issue.
I upload the file i see the file object in my console.log(req.files.filetoupload);
but the when i move the file:

req.files.filetoupload.mv(__dirname + 'upload',function (err) {
        if(err) throw err;
    });

i do not have an err but still the file is not load.
i have sudo permisions

mv function not working but also not giving error

Hi,
I am uploading image file. I am getting file in my server side. but when i execute 'mv' function it's not uploading my file to given destination and its also not giving me any error.

my server side code.

 var pic = req.files.profile_pic;
  var fileName = user_id + '.jpg';
  var newpath = "../public/images/" + fileName;
pic.mv(newpath,function (err) {
            if(err){
                console.log(err);
            }
            else{
                console.log("File uploaded");
            }
        })

it prints file uploaded but file is not on given location 'newpath';

Need proper unit testing

Currently testing the old fashioned way; need a test process in place to eliminate overhead when updating package.

Using with superagent and react

I would like to upload a image file on my server with superagent and react.
But my req.filesis always undefined.

I will appreciate if someone can help me on it :)

Here is my code :

const fileUpload = require('express-fileupload');
app.use(fileUpload())

Server side:

app.post('/uploadprofile', (req, res) => {
        console.log(req.file);
        console.log(req.files);
        console.log(req.body);
        res.status(200).end()
})

Client:

onUploadFile(e) {
        console.log(e.target.files[0])
        this.setState({
            img: e.target.files[0]
        }, () => {
            agent
            .post("http://localhost:3100/uploadprofile")
            .attach('profil', this.state.img, this.state.img.name)
            .set("Content-Type", "")
            .end((err, res) => {
                console.log(err)
                console.log(res)
                console.log("send")
         })
        })
    }

render() {
   return (
        <input id="file" type="file" accept="image/*" name="profil" className="inputButton" 
        onChange={this.onUploadFile}/>
     )
}

In my superagent request, I have to overwrite the content-type, otherwise json data are send.

But the req.file is still undefined on my backend.

Thanks for the help !

Can not save the file, pipe is undefined

Hi, after the upload completes, I try to save the file according to your example using the mv function but I always get this exception:

TypeError: this.data.pipe is not a function
at Object.req.files.(anonymous function).mv (/Users/pam/Documents/pipe/server-file-browser/node_modules/express-fileupload/jslib/index.js:40:25)
at Object.saveSubtitle (/Users/pam/Documents/pipe/server-file-browser/server/src/modules/Files.js:170:22)
at /Users/pam/Documents/pipe/server-file-browser/server/src/app.js:111:11
at Layer.handle as handle_request
at next (/Users/pam/Documents/pipe/server-file-browser/node_modules/express/lib/router/route.js:100:13)
at Route.dispatch (/Users/pam/Documents/pipe/server-file-browser/node_modules/express/lib/router/route.js:81:3)
at Layer.handle as handle_request
at /Users/pam/Documents/pipe/server-file-browser/node_modules/express/lib/router/index.js:227:24
at Function.proto.process_params (/Users/pam/Documents/pipe/server-file-browser/node_modules/express/lib/router/index.js:305:12)
at `/Users/pam/Documents/pipe/server-file-browser/node_modules/express/lib/router/index.js:221:12

I am running on node 4.2.2 and express 4.8.1

Temporary I had to save the data manually.

Thanks for your help!

How to save a file to local destination?

I know this isn't an issue, but please help.
The file is correctly uploading at the server. Its accessible at req.files.filename. But how do i save it to some local destination?

How do I use it with bodyparser

I developed all my project with bodyparser but recently, someone asked me to add a way to upload some files. But I didn't manage to make your project work with mine. I tried something like this :

var express = require('express');
var session = require('express-session');
var hbs = require('hbs');
var bodyParser = require('body-parser');
var fileUpload = require('express-fileupload');

var app = express();
app.set('view engine', 'html');
app.engine('html', hbs.__express);
app.use('/', express.static('public'));
app.use(bodyParser.urlencoded({ extended: true }));
app.use(session({
    secret: 'azerty',
    resave: false,
    saveUninitialized: false
}));
app.use(fileUpload());

app.post('/upload', function(req, res) {
    console.log(req.files);
    if (!req.files) {
        res.send('No files were uploaded.');
        return;
    }
});

with a form generated this way :

var form = $("<form></form>")
            .attr("method", "POST")
            .attr("action", "upload")
            .attr("encType", "multipart/form-data")
            .attr("ref", "uploadForm")
            .attr("id", "uploadForm")
            .attr("postaction", "refreshMenu()")
            .addClass("formulaire");

form.append($("<table></table>")
        .css("text-align", "center")
        .append($("<tr></tr>")
            .append($("<td></td>").html("Choisissez un fichier : "))
            .append($("<td></td>")
                .append($("<input/>")
                    .attr("type", "file")
                    .attr("name", "fichier")
                )
            )
        )
        .append($("<tr></tr>")
            .append($("<td></td>")
                .attr("colspan", "2")
                .append($("<input/>")
                    .attr("type", "submit")
                    .attr("value", "Upload!")
                )
            )
        )
    ).dialog({width: 650, title: "Uploader un fichier", dialogClass: "no-close"});

but when I try to submit my form req.files is always undefined as tells me console.log(req.files); in my server-side code... could you please explain me why ? I expect bodyparser is blocking multipart requests but if it is actually, is there a way to workaround ? And if yes, how ?

safeFileName

When safeFileName is set to true in the options, it removes the dot from the file extension thus rendering the extension useless, it might be an idea to allow dots also.

mv not returning error with missing source file

Hello,
i am currently debugging an issue in an express app using fileupload. In this issue, a user uploads a file but accidentally moves the source file to another directory before uploading it - meaning the file shouldn't be found and the mv method should return an error.
This is happening in Firefox and Chrome, however not in Safari. Can you think of a reason for this?
Thank you for your help.

My setup is:

  • node: v7.6.0
  • express: "^4.14.0"
  • express-fileupload: "0.0.5"

The code using mv is the following:

	uploadedMedia.mv(absolute_path, function(err){
		if (err) {
            res.status(400).send({result:false, error:err});
        }else{
        	res.status(200).send({result:true});
        	if(extension == 'wav'){
        		var mp3_path = absolute_path.slice(0, -3) + "mp3";

        		var command = 'lame ' + absolute_path + " " + mp3_path + " && rm " + absolute_path;
        		cmd.run(command);
        		console.log(command);
        	}
        }
	});

Can safely save a file of arbitrary name?

Is there any provision to check whether a filename is valid, or is this something I have to do explicitly?

For example, in my server.js:

app.post('/upload-file', function(req, res) {
  var filename = req.files.file.name;
  req.files.file.mv(__dirname + '/public/img/' + filename, function(err) {
    if(err) {
      res.json({err: err});
    }
    else {
      res.json({answer: "File transfer completed"});    
    }
  });
});

In this example, if the user's filename was ../../server.js, this could really mess things up badly.

mv function error

when I upload image, return this error
{ "errno": -2, "code": "ENOENT", "syscall": "open", "path": "public/images/visitor7.jpg" }
please help me ~~

"errno":-4058,"code":"ENOENT","syscall":"open"

Hi, I am using sampleFile.mv. It was working before but recently I am seeing below exception. I am using Postman tool to make a call. I tried using RestClient tool and seeing same error.

Response:
{
"errno": -4058,
"code": "ENOENT",
"syscall": "open",
"path": "C:\Users\QATools\testdb-api\Users\u000bn0u7yzDesktopOmniture excels files - Copy\omniture-test-data.xlsx"
}

upload api issue

Below is the code i am using:-
var express = require('express')
const fileUpload = require('express-fileupload');
app.post('/uploadfile', function(req, res) {
if (!req.files)
return res.status(400).send('No files were uploaded.');

// The name of the input field (i.e. "sampleFile") is used to retrieve the uploaded file

let sampleFile = req.files.file;
console.log(req.files.file);

var filename = req.files.file['name'];
console.log(filename);
// Use the mv() method to place the file somewhere on your server
sampleFile.mv('C:\Users\user\Desktop\Omniture excels files - Copy/'+filename, function(err) {
if (err)
return res.status(500).send(err);

res.send('File uploaded!');

});
});

SyntaxError: Unexpected token =

I am unable to run my application with express-fileupload installed. I get the following error when attempting to run my application:

/node_modules/express-fileupload/lib/index.js:137
mv: function(path, callback = null) {
^

SyntaxError: Unexpected token =
at exports.runInThisContext (vm.js:53:16)
at Module._compile (module.js:373:25)
at Object.Module._extensions..js (module.js:416:10)
at Module.load (module.js:343:32)
at Function.Module._load (module.js:300:12)
at Module.require (module.js:353:17)
at require (internal/module.js:12:17)
at Object. (/.js:10:16)
at Module._compile (module.js:409:26)
at Object.Module._extensions..js (module.js:416:10)

Here is my route code:

app.post("/admin/hoverimage/upload/:id", function(req, res){
			if (!req.files)
    	return res.status(400).send("No files were uploaded.");
			console.log(req.body);
			// The name of the input field (i.e. "sampleFile") is used to retrieve the uploaded file
  		let hoverimage = req.files.hoverimage;
			let path = "/media/"+hoverimage.name;
			hoverimage.mv(path, function(err) {
				if (err)
					return res.status(500).send(err);
				res.send("File uploaded!");
			});
			//res.redirect("/admin/gallery/images/"+id);
		});

Please advise.

SyntaxError: Use of const in strict mode.

Error:

node_work/node_modules/express-fileupload/lib/index.js:3
const Busboy = require('busboy');
^^^^^
SyntaxError: Use of const in strict mode.

I am getting this error. If I replace constant with var then it gives new error

node_work/node_modules/express-fileupload/lib/index.js:35
  let busboyOptions = {};
  ^^^
SyntaxError: Unexpected strict mode reserved word

limit file size per action not whole app

Hello,
is there a way to limit /check the file size per route call? I have various places in my application where I upload files, and the size limit is different for each of them I do it in the client, but want to double-check in the server. Is there a way I can check the file size in the route call so I do what I need to do ?
Thanks

Adding an additional property to files!

Working on uploading a a complete folder to the server. Need webkitRelativePath property of the file to preserver directory structure. Please add this functionality or point me in the right direction, i will do it myself.

Heads Up: Ajax Post hangs when initializing Fileupload in the wrong order.

I have a page with the following ajax post request:

$.ajax({
  type: "POST",
  url: "/my/some_id/path/create",
  data: some_data,
  success: function () {
    //Do something
  },
  fail: function () {
    console.log("fail");
  }
});

It was working just fine until a couple days ago, just before I tried to use this library. The library did indeed work as expected, but using it seemed to have negative consequences on existing functionality. The previously mentioned request hanged for about a minute. Then, although the browser would not receive anything, my app would raise this error:

Error: request aborted
  at IncomingMessage.onAborted (/home/cavpollo/my_app/node_modules/body-parser/node_modules/raw-body/index.js:269:10)
  at emitNone (events.js:68:13)
  at IncomingMessage.emit (events.js:167:7)
  at IncomingMessage.wrapped (/home/cavpollo/my_app/node_modules/newrelic/lib/transaction/tracer/index.js:161:28)
  at IncomingMessage.wrappedEmit [as emit] (/home/cavpollo/my_app/node_modules/newrelic/lib/transaction/tracer/index.js:198:46)
  at abortIncoming (_http_server.js:281:11)
  at Socket.serverSocketCloseListener (_http_server.js:294:5)
  at emitOne (events.js:83:20)
  at Socket.emit (events.js:170:7)
  at TCP._onclose (net.js:470:12)

After some debugging I came to realize that the problem was this line in my app initialization:

var express = require('express');
var fileUpload = require('express-fileupload');
var bodyParser = require('body-parser');
var app = express();

//Some irrelevant code

app.use(fileUpload()); // <------------- This line

//Some more irrelevant code

app.use(bodyParser.urlencoded({extended: false}));
app.use(bodyParser.json());

Just commenting the line allowed me to do the Ajax Post call without problems... so then I thought, what if we switch things around?

var express = require('express');
var fileUpload = require('express-fileupload');
var bodyParser = require('body-parser');
var app = express();

//Some irrelevant code again

app.use(bodyParser.urlencoded({extended: false}));
app.use(bodyParser.json());
app.use(fileUpload());

It turns out that initializing things this way results in no errors and things working as expected.

Just a friendly heads up to anybody who stumbles on this problem. I was using version express-fileupload verison 0.0.5, express 4.14.0 and body-parser 1.13.3, if it is of any help.

Is it possible to change filename?

Currently working REST API for uploading the documents to user profile. Is there anyway to change the file name before moving to particular folder. Like I need to change the file name dynamically example for user profile name need to store based userId.

Recommend Projects

  • React photo React

    A declarative, efficient, and flexible JavaScript library for building user interfaces.

  • Vue.js photo Vue.js

    ๐Ÿ–– Vue.js is a progressive, incrementally-adoptable JavaScript framework for building UI on the web.

  • Typescript photo Typescript

    TypeScript is a superset of JavaScript that compiles to clean JavaScript output.

  • TensorFlow photo TensorFlow

    An Open Source Machine Learning Framework for Everyone

  • Django photo Django

    The Web framework for perfectionists with deadlines.

  • D3 photo D3

    Bring data to life with SVG, Canvas and HTML. ๐Ÿ“Š๐Ÿ“ˆ๐ŸŽ‰

Recommend Topics

  • javascript

    JavaScript (JS) is a lightweight interpreted programming language with first-class functions.

  • web

    Some thing interesting about web. New door for the world.

  • server

    A server is a program made to process requests and deliver data to clients.

  • Machine learning

    Machine learning is a way of modeling and interpreting data that allows a piece of software to respond intelligently.

  • Game

    Some thing interesting about game, make everyone happy.

Recommend Org

  • Facebook photo Facebook

    We are working to build community through open source technology. NB: members must have two-factor auth.

  • Microsoft photo Microsoft

    Open source projects and samples from Microsoft.

  • Google photo Google

    Google โค๏ธ Open Source for everyone.

  • D3 photo D3

    Data-Driven Documents codes.