如何让用户在 Web 应用程序中一次上传一定数量的图片?

How do I make the user upload a set amount of pictures at once in a web app?

我有一个用 Node.js 编写的网络应用程序,我希望用户能够在他们使用手机拍摄一定数量的照片时提交他的图像,例如20张图片(他们不能提交更多或更少)。我知道我可以使用以下行在 html 中收集图像,但不太清楚如果用户拍摄了 x 张照片规则如何强制执行唯一上传。

<input type="file" accept="image/*" capture="user">

您可以参考示例, 您需要在 HTML 中使用 'multiple' 关键字作为,

<input type="file" name="userPhoto" multiple />

恕我直言,Multer 是处理文件上传的优秀 npm 包之一。 您可以配置编号。您希望允许上传的文件数量

 var upload = multer({ storage : storage }).array('userPhoto',20);

示例Node.JS代码

var bodyParser = require("body-parser");
var multer = require('multer');
var app = express();

app.use(bodyParser.json());

var storage = multer.diskStorage({
  destination: function (req, file, callback) {
    callback(null, './uploads');
  },
  filename: function (req, file, callback) {
    callback(null, file.fieldname + '-' + Date.now());
  }
});

var upload = multer({ storage : storage }).array('userPhoto',20); // here you can set file limit to upload

app.get('/',function(req,res){
      res.sendFile(__dirname + "/index.html");
});

app.post('/api/photo',function(req,res){
    upload(req,res,function(err) {
        //console.log(req.body);
        //console.log(req.files);
        if(err) {
            return res.end("Error uploading file.");
        }
        res.end("File is uploaded");
    });
});

app.listen(3000,function(){
    console.log("Working on port 3000");
});

要允许上传多个文件,您必须在 input 标签中包含 multiple 属性。

<input type="file" accept="image/*" capture="user" multiple="true" id="file-inp" />

您应该验证上传到客户端的文件数量,并在错误操作时提醒用户。

$('#file-inp').change(function() {
    var files = $(this)[0].files;
    if(files.length != 20){
        alert("only a total of 20 files should be uploaded");
    }
    //enter code here
});

最简单的解决方案

您可以简单地从 input.files.length.

中获取输入中选择的文件数

此代码段应该对您有所帮助。我在代码中插入了注释来澄清它,注释还包含您应该使用以获得更好体验的代码。

如果您不是 ES6 人,请查看下面的代码段。另外,请查看此答案底部的 进一步阅读 部分。

const theInput = document.getElementById('theInput');
const theChooser = document.getElementById('theChooser');

let theNumber; //Or "let theNumber = your-default-number" if you want to avoid possible error when user selected 0 files

theChooser.oninput = () => {
  theNumber = theChooser.value * 1 //Multiplying it to 1 will make the (already number) input a number anyways
}

theInput.onchange = () => {
  theInput.files.length === theNumber ? theFunction() : theWarning()
  // To do nothing when user selected 0 files (user clicked Cancel) do this
  /*
  theInput.files.length === theNumber ? theFunction() : theInput.files.length === 0 ? null : theWarning()
  */
}

function theFunction() {
  console.log(`You chose exactly ${theNumber} files. Yay!`);
}

function theWarning() {
  console.log(`¯\_(ツ)_/¯ Watcha doing, you told to choose only ${theNumber} files.`);
}
<input type="number" id="theChooser" placeholder="Set your number of files">
<br>
<input type="file" id="theInput" multiple>

不熟悉 ES6,请参阅此片段。比较这两个片段将帮助您熟悉 ES6。

const theInput = document.getElementById('theInput');
const theChooser = document.getElementById('theChooser');

let theNumber; //Or "let theNumber = your-default-number" if you want to avoid possible errors when user selected 0 files

theChooser.addEventListener('input', function() {
  theNumber = theChooser.value * 1; //Multiplying it to 1 will make the (already number) input a number anyways
})

theInput.addEventListener('change', function() {
  if (theInput.files.length === theNumber) {
    theFunction();
  } else {
    theWarning();
  }
  // To do nothing when user selected 0 files (user clicked Cancel) do this
  /*
  if (theInput.files.length === theNumber) {
    theFunction();
  } else if (theInput.files.length === 0) {
  } else {
    theWarning();
  }
  */
})

function theFunction() {
  console.log("You chose exactly " + theNumber + " files. Yay!");
}

function theWarning() {
  console.log("¯\_(ツ)_/¯ Watcha doing, you told to choose only " + theNumber + " files.");
}
<input type="number" id="theChooser" placeholder="Set your number of files">
<br>
<input type="file" id="theInput" multiple>

进一步阅读