Im new to node js ora voglio fare upload.immagine ho scaricato framework express per gestire l’upload.Per favore aiutarmi come gestire quel caricamento nel lato server.
Ho creato un modulo come questo per gestire questo nel nodo back-end js
Usa questo metodo per il caricamento
app.post('/upload', function(req, res) { // get the temporary location of the file var tmp_path = req.files.thumbnail.path; // set where the file should actually exists - in this case it is in the "images" directory target_path = '/tmp/' + req.files.thumbnail.name; // move the file from the temporary location to the intended location fs.rename(tmp_path, target_path, function(err) { if (err) throw err; // delete the temporary file, so that the explicitly set temporary upload dir does not get filled with unwanted files fs.unlink(tmp_path, function() { if (err) throw err; }); }); });
Mentre il retriving mostra questo percorso in questo metodo
fs.readFile(target_path, "binary", function(error, file) { if(error) { res.writeHead(500, {"Content-Type": "text/plain"}); res.write(error + "\n"); res.end(); } else { res.writeHead(200, {"Content-Type": "image/png"}); res.write(file, "binary"); } });
Consultare nodejs expressjs caricare le immagini e visualizzarle per maggiori dettagli