Upload a file using FS and NodeJS

拟墨画扇 提交于 2019-12-12 05:09:10

问题


I get an error when I try to upload a file, it works in local but it doesn't work on my remote server...

My error :

    [sbaf.fr.3005-53 (out) 2014-03-05T20:19:59] { [Error: ENOENT, rename '/tmp/1e426309d298d9ab1d099e1017584058']
[sbaf.fr.3005-53 (out) 2014-03-05T20:19:59]   errno: 34,
[sbaf.fr.3005-53 (out) 2014-03-05T20:19:59]   code: 'ENOENT',
[sbaf.fr.3005-53 (out) 2014-03-05T20:19:59]   path: '/tmp/1e426309d298d9ab1d099e1017584058' }

My controller :

photoDAL.prototype.save = function(photo, file, callback) {
    photo.file = file.name;
    var photo = dbContext.photo.build(photo);

    var file_tmp = file.path;
    var file_name = file.name;
    var file_type = file.type;
    var file = './public/images/photo/'+file_name;

    fs.rename(file_tmp, file, function(err){
        if( err ) console.log(err);
    });

    photo.save().success(function(photo) {
        callback(photo);
    }).error(function(error) {
        callback({message: error});
    });
};

EDIT #1 :

Screenshots of my ExpressJS app...

Screenshot 1 : http://glui.me/?i=eweyq4ovennej50/2014-03-05_at_20.34_2x.png/ Screenshot 2 : http://glui.me/?i=1n2cjv57jd2fmwq/2014-03-05_at_20.33_2x.png/

EDIT #2 :

My code :

console.log(process.cwd());
console.log(__dirname);

The console :

[sbaf.fr.3005-71 (out) 2014-03-05T21:55:48] /home/anthoc/apps
[sbaf.fr.3005-71 (out) 2014-03-05T21:55:48] /home/anthoc/apps/sbaf.fr/app/dal

回答1:


So this: var file = './public/images/photo/'+file_name; is a relative path based on process.cwd(). Presumably if your server process was started with your app repo root as the cwd, all should be well, but probably that is not the case. It's more robust to not rely on the cwd but use __dirname and construct paths relative to the location of the current javascript file. So give that a try and see if it fixes it. You can confirm one way or another with: console.log(process.cwd()) in your controller module to debug this.




回答2:


You can try using __dirname. Here is how I would do it:

photoDAL.prototype.save = function(photo, file, callback) {
photo.file = file.name;
var photo = dbContext.photo.build(photo);

var file_tmp = file.path;
var file_name = file.name;
var file_type = file.type;
var file = __dirname + '/public/images/photo/'+file_name;

fs.rename(file_tmp, file, function(err){
    if( err ) console.log(err);
});

photo.save().success(function(photo) {
    callback(photo);
}).error(function(error) {
    callback({message: error});
});

};

Now, if you're code is in a folder and your target is in the parent, like me:

-root
  - public
     -photos
  -server
     -upload.js

You can add a function to the string proto.

String.prototype.getParent = function () {
        // Be cross-platform
    var replaced = this.replace(new RegExp("\\\\", "g"), '/');
    var index = replaced.lastIndexOf('/');
    return replaced.substring(0, index);
};

Now call __dirname.getParent() as many time as needed (__dirname.getParent().getParent()...).




回答3:


You can use formidable module. easy to use

https://www.npmjs.org/package/formidable



来源:https://stackoverflow.com/questions/22207281/upload-a-file-using-fs-and-nodejs

易学教程内所有资源均来自网络或用户发布的内容,如有违反法律规定的内容欢迎反馈
该文章没有解决你所遇到的问题?点击提问,说说你的问题,让更多的人一起探讨吧!