“Загрузите узел JS” Ответ

Node.js загрузить файлы

// create upload form

var http = require('http');

http.createServer(function (req, res) {
  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);

// parse upload file

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

http.createServer(function (req, res) {
  if (req.url == '/fileupload') {
    var form = new formidable.IncomingForm();
    form.parse(req, function (err, fields, files) {
      res.write('File uploaded');
      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();
  }
}).listen(8080);

// save file

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 = 'C:/Users/Your Name/' + files.filetoupload.originalFilename;
      fs.rename(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();
  }
}).listen(8080);
Astra Logical

Загрузите узел JS

const formidable = require('formidable');
const fs = require('fs');
const express = require('express');
const app = express();
const http = require('http');
const server = http.createServer(app);
const hbjs = require('handbrake-js')
const cors = require('cors');
bodyParser = require('body-parser');
app.use(bodyParser.json());
app.use(bodyParser.urlencoded({ extended: true }));
app.use(cors());

app.post('/upload', (req, res) => {
    let form = new formidable.IncomingForm();
    form.parse(req, function (error, fields, file) {
    console.log(file)
    const filepath = file.fileupload.filepath;
    const newpath = 'upload/';
    newpath += file.fileupload.originalFilename;
    if (fs.existsSync(newpath)) {
    // path exists
    console.log("exists:", newpath);
    } else {
    console.log("DOES NOT exist:", newpath);
    }

    fs.rename(filepath, newpath, function () {
    res.write('NodeJS File Upload Success!');
    res.end();
    });
  
    let randnum = Math.floor(Math.random())
    hbjs.spawn({ input: `${file.fileupload.originalFilename}`, output: `file${randnum}.m4v` })
    .on('error', err => {
        console.log(err)
    })
    .on('progress', progress => {
        console.log
        (
        'Percent complete: %s, ETA: %s',
        progress.percentComplete,
        progress.eta
        )
    })
});
})

app.get('/', (req, res) => {
res.sendFile(__dirname + '/upload.html');
});

server.listen(8086, () => {
    console.log('listening on *:1034'); 
});
Maeron Reyes

Ответы похожие на “Загрузите узел JS”

Вопросы похожие на “Загрузите узел JS”

Больше похожих ответов на “Загрузите узел JS” по JavaScript

Смотреть популярные ответы по языку

Смотреть другие языки программирования