0

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

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

3 回答 3

1

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

<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
});
于 2020-08-14T12:45:49.317 回答
1

您可以将示例引用为,您需要在 HTML 中使用“多个”关键字,

<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");
});
于 2020-08-14T12:38:06.960 回答
1

Simplest solution

You can get the number of files selected in the input simply from input.files.length.

This code snippet should help you. I have inserted comments in code to clarify it, comments also contain code you should use for a better experience.

If you are not an ES6 person check out the snippet below. Also, check out the Further Reading section at the bottom of this answer.

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>

Not familiar with ES6, see this snippet. Comparing both snippets will help you get familiar with 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>

Further Reading

于 2020-08-14T13:11:38.290 回答