I have problem upload file nodejs. i have read
stack1
and
stack2. but i think this different case
on terminal output like this :
> Unhandled rejection Error: EXDEV: cross-device link not permitted,
> rename '/tmp/31941-53enly.png' ->
> 'public/files/complaint/Screenshot_2016-05-01_01-16-55.png'
> at Error (native)
> at Object.fs.renameSync (fs.js:681:18)
> at null.<anonymous> (/home/faris/myprojects/orchestrale-server/routes/complaintimage.js:163:20)
> at tryCatcher (/home/faris/myprojects/orchestrale-server/node_modules/sequelize/node_modules/bluebird/js/release/util.js:16:23)
> at Promise._settlePromiseFromHandler (/home/faris/myprojects/orchestrale-server/node_modules/sequelize/node_modules/bluebird/js/release/promise.js:502:31)
> at Promise._settlePromise (/home/faris/myprojects/orchestrale-server/node_modules/sequelize/node_modules/bluebird/js/release/promise.js:559:18)
> at Promise._settlePromise0 (/home/faris/myprojects/orchestrale-server/node_modules/sequelize/node_modules/bluebird/js/release/promise.js:604:10)
> at Promise._settlePromises (/home/faris/myprojects/orchestrale-server/node_modules/sequelize/node_modules/bluebird/js/release/promise.js:683:18)
> at Async._drainQueue (/home/faris/myprojects/orchestrale-server/node_modules/sequelize/node_modules/bluebird/js/release/async.js:138:16)
> at Async._drainQueues (/home/faris/myprojects/orchestrale-server/node_modules/sequelize/node_modules/bluebird/js/release/async.js:148:10)
> at Immediate.Async.drainQueues [as _onImmediate] (/home/faris/myprojects/orchestrale-server/node_modules/sequelize/node_modules/bluebird/js/release/async.js:17:14)
> at processImmediate [as _immediateCallback] (timers.js:383:17)
my code :
if (_.isEmpty(req.files) == false) {
var tp = avatar_folder+req.files.file.name;
fs.renameSync(req.files.file.path, tp, function(err){});
var avatar = req.files.file.name;
}
code work on another distro like elementary os, but when i run project on debian jessie or ubuntu 16 LTS that error result on terminal.
any body can’t help ? that code just run perfectly on elementary os and arch.
8 Answers
Same problem. Workaround: Read the temp file, write the file in new location and remove the temp file:
// Read the file
fs.readFile(oldpath, function (err, data) {
if (err) throw err;
console.log('File read!');
// Write the file
fs.writeFile(newpath, data, function (err) {
if (err) throw err;
res.write('File uploaded and moved!');
res.end();
console.log('File written!');
});
// Delete the file
fs.unlink(oldpath, function (err) {
if (err) throw err;
console.log('File deleted!');
});
});
You can use ‘mv’ packgage to resolve this issue
Link NPM: https://www.npmjs.com/package/mv
How to use:
Replace some old code with fs.rename:
fs.rename(temp_path, target_path, (err) => {
if (err) {
console.log('> FileServer.jsx | route: "/files/upload" | err:', err);
throw err;
}
});
With:
const mv = require('mv');
mv(temp_path, target_path, function (err) {
if (err) {
console.log('> FileServer.jsx | route: "/files/upload" | err:', err);
throw err;
}
});
var http = require('http');
var formidable = require('formidable');
var fs = require('fs');
var mv = require('mv');
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.path;
console.log(oldpath);
var newpath = 'F:/change path to your project dir/' + files.filetoupload.name;
mv(oldpath, newpath, function (err) {
if (err) throw err;
res.write('File uploaded and moved!');
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('<input type="submit">');
res.write('</form>');
return res.end();
}enter code here
}).listen(8080);
In order to resolve this problem, you can rewrite as:
fs.writeFile(path_where_to_write, file, function(err) {
if (err) throw err; /*do something else.*/
});
/*and then Remove the file from tmp location*/
fs.unlink(tempPath);
and for multiple files, you could iterate this block based on length of files.
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.path;
console.log(oldpath);
var newpath = 'F:/your folder name/' + files.filetoupload.name;
fs.readFile(oldpath, function (err, data) {
if (err) throw err;
console.log('File read!');
// Write the file
fs.writeFile(newpath, data, function (err) {
if (err) throw err;
res.write('File uploaded and moved!');
res.end();
console.log('File written!');
});
// Delete the file
fs.unlink(oldpath, function (err) {
if (err) throw err;
console.log('File deleted!');
});
});
});
} 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('<input type="submit">');
res.write('</form>');
return res.end();
}
}).listen(8080);
You have to use fs.writeFileSync
otherwise oldfile can be deleted before copied
fs.writeFileSync(newpath, data, function (err) {
if (err) throw err;
res.write('File uploaded and moved!');
res.end();
console.log('File written!');
});
// Delete the file
fs.unlink(oldpath, function (err) {
if (err) throw err;
console.log('File deleted!');
});
yes, you can resole this problem as.
-
install mv by command
npm install mv --save
-
add mv in your nodejs file
var mv = require('mv');
-
move file as:
mv('source_file', 'destination_file,' function(err) { if (err) { throw err; } console.log('file moved successfully'); };
As part of developing for Cloud66, I have a whale of a time writing many a Dockerfile.
Today, I thought I’d share some of the more weird or painful issues that I have experienced while trying to get various Node projects to work with Docker. I will be on cloud 9 if by virtue of reading this article you avoid any one of these in the future!
Without further ado (and terrible puns!) follows a short list.
EXDEV: cross-device link not permitted
If you try update npm 3.x in an image that already has npm installed (docker run mhart/alpine-node:5 npm install/update -g npm
); install certain packages, such as gulp, twice; or run npm update
inside the Dockerfile, you may experience this error. More specifically, this happens whenever the installation process for a package uses fs.rename
.
A clever solution, suggested by nodrluf [2], is to add the following to your Dockerfile (it is important that they are part of the same RUN command, as you will see shortly)
RUN mv ./node_modules ./node_modules.tmp
&& mv ./node_modules.tmp ./node_modules
&& npm install (or whichever command caused your failure)
The reason this works is a bit long-winded. For one, it only happens when you are using the AUFS filesystem with Docker. AUFS is a layered filesystem. For a Docker image, at any one point there are many read-only AUFS layers corresponding to the Docker image layers, with a writable layer on top where the operations take place (either as part of creating the full Docker image, or running a Docker container). The union of these layers gives the complete picture of the filesystem.
When you try to run the rename
system call on AUFS, it cannot do so in the read-only layers — they’re read-only! It must copy all the data it wants to use to the current layer from the read-only layer, and then perform the operation. Since the read-only layer could require data from another read-only layer below it, and so on, this is a potentially very expensive operation.
If AUFS deems the operation too expensive, it throws an EXDEV
error [1]. Since your application does not know how to handle this error, we end up with our current predicament.
However, mv
does know how to handle the EXDEV
error correctly, and uses system calls other than rename
. The end result is that the node_modules
folder is in now in the writable layer, and you no longer get the EXDEV
error in future operations. Yay!
Bower installation does not run with root!
The solution seems simple enough — bower
has an --allow-root
flag.
But what if bower install
happens as part of, say, grunt init
? Not so easy to pass flags anymore.
A possible solution is to add the following to your Dockerfile, which affects the global config of bower.
RUN echo '{ "allow_root": true }' > /root/.bowerrc
Alternatively, you could perform the build with a non-root user — create one with adduser
and use the USER
directive in your Dockerfile to change user. You’d likely have to fumble a bit with permissions, but it seems to be the technically superior solution.
Containers are ephemeral!
More than once I have seen projects that create config files (or even plain text password files!) at runtime. This is usually to accommodate a pretty installation page on the illustrious port 80.
This is all great and stuff, and works fine for installation on a server rather than a container. But what if your container crashes? Or you want to move it to another server? You runtime generated files will be lost. As such, you should either commit any required files for your containers operation to your application’s repository, or generate them during the build. If you have any sensitive information, use environment variables.
References
[1] http://aufs.sourceforge.net/aufs.html
[2] https://github.com/npm/npm/issues/9863
When writing a file upload function,Error calling fs.renamesync method
The error code is as follows:
function upload (response, request) {
console.log ("upload called");
var form=new formidable.incomingform ();
console.log ("about to parse");
form.parse (request, function (error, fields, files) {
console.log ("parsing done");
fs.renamesync (files.upload.path, "./tmp/test.jpg");
response.writehead (200, {"content-type":"text/html"});
response.write ("received image:<br />");
response.write ("<img src ="/show "/>");
response.end ();
});
}
After a rough analysis,This is expected to be caused by permissions issues when moving or manipulating files across disk partitions.
Here are two solutions:
method one:
Mainly use the createreadstream, createwritesream and unlinksync methods of fs
The specific code is as follows:
function upload (response, request) {
console.log ("upload called");
var form=new formidable.incomingform ();
console.log ("about to parse");
form.parse (request, function (error, fields, files) {
console.log ("parsing done");
//fs.renamesync (files.upload.path, "./tmp/test.jpg");
var readstream=fs.createreadstream (files.upload.path);
var writestream=fs.createwritestream ("./tmp/test.jpg");
readstream.pipe (writestream);
readstream.on ("end", function () {
fs.unlinksync (files.upload.path);
});
response.writehead (200, {"content-type":"text/html"});
response.write ("received image:<br />");
response.write ("<img src ="/show "/>");
response.end ();
});
}
ps:The node version I use is 0.10.69. If I use a version below 0.6,You can use util.pump
The corresponding code just needs to change the readstream.on in the above code to:(note the introduction of the util module)
util.pump (readstream, writestream, function () {
fs.unlinksync ("files.upload.path");
});
Method Two:
This is much simpler
Add a form.uploaddir=»tmp» (write a temporary path)
function upload (response, request) {
console.log ("upload called");
var form=new formidable.incomingform ();
form.uploaddir="tmp";
console.log ("about to parse");
form.parse (request, function (error, fields, files) {
console.log ("parsing done");
fs.renamesync (files.upload.path, "./tmp/test.jpg");
response.writehead (, {"content-type":"text/html"});
response.write ("received image:<br />");
response.write ("<img src ="/show "/>");
response.end ();
});
}
Hello,
Whenever I open photoshop or go to export my work, i’m greeted with the «There is a problem with Generator. Please quit Photoshop and try again. If the problem persists, remove any third-party plug-ins or try to reinstalling Photoshop.» Any help would be greatly appreciated, since PS is unusable at this point.
I’ve tried the two official solutions to get this resolved, but neither seemed to work. I’ve tried assigning full permissions to the adobe and photoshop folder in my profile, program directories, Just deleting the folders in my profile, deleting my profile and reinstalling PS, deleting my profile and adding user to the administrator group then installing PS, Running the cleanup tool and reloading PS and finally last week I thought reloading windows 10 would finally fix this issue, but it didn’t.
I can uncheck the Generator plugin and that only makes the dialog go away until the next time I try and export. When i do finally do an export, I get the error message and it just sits processing and can never actually export my work. Is there any way to just force disable generator? I’m running windows 10 x64 (1709) and photoshop 2018 (19.0.1), but i’ve had this issue for sometime. Let me know if there is any additional info or logs that I can add or something I could try to get these images exported.
This is the error I see in the generator_laest log:
warning:crema 09:34:24.360 C:Program FilesAdobeAdobe Photoshop CC 2018RequiredPlug-InsGeneratorcrema.generatelibfile-utilsindex.js:73:28] Unable to move file. Copying instead: { [Error: EXDEV: cross-device link not permitted, rename ‘C:UsersusernameAppDataLocalTemptmp-3876mYqAjGHwLEnO’ -> ‘N:Folderfolderfolderfolderfolderimage.png’]
errno: -4037,
code: ‘EXDEV’,
syscall: ‘rename’,
path: ‘C:\Users\username\AppData\Local\Temp\tmp-3876mYqAjGHwLEnO’,
dest: ‘N:\Folder\folder\folder\folder\folder\image.png’ }
Here is some output from generator_1:
[info:core 09:10:12.649 C:Program FilesAdobeAdobe Photoshop CC 2018RequiredGenerator-builtinlibgenerator.js:234:25] Detected Photoshop version: 19.0.1
[1;31mNo viable plugins were found in ‘C:Program FilesAdobeAdobe Photoshop CC 2018Plug-InsGenerator’
[0m[info:core 09:10:12.697 C:Program FilesAdobeAdobe Photoshop CC 2018RequiredGenerator-builtinlibgenerator.js:1903:21] Loading plugin: generator-assets (v2.9.0) from directory: C:Program FilesAdobeAdobe Photoshop CC 2018RequiredPlug-InsGeneratorassets.generate
[warning:core 09:10:13.716 C:Program FilesAdobeAdobe Photoshop CC 2018RequiredGenerator-builtinlibgenerator.js:892:21] WARNING the imageChanged event is expensive, please consider NOT listening to it
[info:core 09:10:13.723 C:Program FilesAdobeAdobe Photoshop CC 2018RequiredGenerator-builtinlibgenerator.js:1919:21] Plugin loaded: generator-assets
[info:core 09:10:13.730 C:Program FilesAdobeAdobe Photoshop CC 2018RequiredGenerator-builtinlibgenerator.js:1903:21] Loading plugin: crema (v3.0.13) from directory: C:Program FilesAdobeAdobe Photoshop CC 2018RequiredPlug-InsGeneratorcrema.generate
[info:core 09:10:15.802 C:Program FilesAdobeAdobe Photoshop CC 2018RequiredGenerator-builtinlibgenerator.js:1919:21] Plugin loaded: crema
[warning:core 09:10:16.253 C:Program FilesAdobeAdobe Photoshop CC 2018RequiredGenerator-builtinlibgenerator.js:892:21] WARNING the imageChanged event is expensive, please consider NOT listening to it
[1;31muser settings file could not be created
[0m[warning:core 09:10:17.769 C:Program FilesAdobeAdobe Photoshop CC 2018RequiredGenerator-builtinlibgenerator.js:129:25] Photoshop error { id: 18, body: ‘Unknown JavaScript error’ }
[info:crema 09:10:18.423 C:Program FilesAdobeAdobe Photoshop CC 2018RequiredPlug-InsGeneratorcrema.generatemain.js:343:17] Accepted websocket connection with ID 0
[0mGET /handshake [32m200 [0m4.871 ms — — [0m
[info:crema 09:10:18.455 C:Program FilesAdobeAdobe Photoshop CC 2018RequiredPlug-InsGeneratorcrema.generatemain.js:301:17] initial message recieved
[info:crema 09:10:18.472 C:Program FilesAdobeAdobe Photoshop CC 2018RequiredPlug-InsGeneratorcrema.generatemain.js:165:21] received command: docinfo
[info:crema 09:10:18.877 C:Program FilesAdobeAdobe Photoshop CC 2018RequiredPlug-InsGeneratorcrema.generatemain.js:165:21] received command: generatePreview
[info:core 09:10:19.028 C:Program FilesAdobeAdobe Photoshop CC 2018RequiredGenerator-builtinlibphotoshop.js:488:26] 35ms to receive 12304.7 kB (351563.5 kB/s)
[info:core 09:10:19.087 C:Program FilesAdobeAdobe Photoshop CC 2018RequiredGenerator-builtinlibconvert.js:231:20] Using Flite for image encoding
[0mGET /preview/1.png?t=1515165018875 [32m200 [0m2.699 ms — — [0m
[info:crema 09:10:40.192 C:Program FilesAdobeAdobe Photoshop CC 2018RequiredPlug-InsGeneratorcrema.generatemain.js:356:21] WebSocket connection with ID 0 closed
[warning:core 09:10:42.469 C:Program FilesAdobeAdobe Photoshop CC 2018RequiredGenerator-builtinlibgenerator.js:129:25] Photoshop error Pipe read error: Error: EPIPE: broken pipe, read
[info:core 09:10:42.474 C:Program FilesAdobeAdobe Photoshop CC 2018RequiredGenerator-builtinlibgenerator.js:124:25] Photoshop connection closed
[info:core 09:10:42.481 C:Program FilesAdobeAdobe Photoshop CC 2018RequiredGenerator-builtinlibgenerator.js:124:25] Photoshop connection closed
[1;31mExiting with code 0: Generator close event
[0m