【问题标题】:Add more files separately using jquery [duplicate]使用jquery单独添加更多文件[重复]
【发布时间】:2017-08-29 11:42:10
【问题描述】:

我的用户可以单独添加更多文件。现在我可以一次添加多个文件,但我不能选择它要替换的其他文件。当用户添加一个文件时,应该有添加更多文件的选项。我怎样才能做到这一点?在这里,我只为这个表单添加了上传脚本。

PHP

if($_FILES['fil_attachment']['name'] != '') {

    @copy(
        $_FILES['fil_attachment']['tmp_name'],
        'uploads/'.$_FILES['fil_attachment']['name']
    );
    $attachment[0] = "uploads"."/".$_FILES['fil_attachment']['name'];

    $file_name=$_FILES['fil_attachment']['name'];
}

HTML

<form 
    class="form-request" 
    name="frmRequest"  
    method="post" 
    action 
    enctype="multipart/form-data"
    >

    <input 
        name="MyFile" 
        type="hidden" 
        id="MyFile" 
        tabindex="99" 
        size="1" />

    <input 
        type="file" 
        name="fil_attachment" 
        multiple 
        id="fil_attachment" 
        onChange="MyFile.value=fil_attachment.value" />

    <input type="submit">
</form>

【问题讨论】:

    标签: php jquery html file-upload


    【解决方案1】:

    请参阅此处上传多个文件的 PHP 手册部分: http://php.net/manual/en/features.file-upload.multiple.php

    另外,看看这个现有的问题: Multiple file upload in php

    【讨论】:

      【解决方案2】:

      JavaScript 和 jQuery 帮助您选择多个上传项目。这是代码sn-p

      $(function() {
        var countFiles = 1,
          $body = $('body'),
          typeFileArea = ['txt', 'doc', 'docx', 'ods'],
          coutnTypeFiles = typeFileArea.length;
      
        //create new element
        $body.on('click', '.files-wr label', function() {
          var wrapFiles = $('.files-wr'),
            newFileInput;
      
          countFiles = wrapFiles.data('count-files') + 1;
          wrapFiles.data('count-files', countFiles);
      
          newFileInput = '<div class="one-file"><label for="file-' + countFiles + '">Attach file</label>' +
            '<input type="file" name="file-' + countFiles + '" id="file-' + countFiles + '"><div class="file-item hide-btn">' +
            '<span class="file-name"></span><span class="btn btn-del-file">x</span></div></div>';
          wrapFiles.prepend(newFileInput);
        });
      
        //show text file and check type file
        $body.on('change', 'input[type="file"]', function() {
          var $this = $(this),
            valText = $this.val(),
            fileName = valText.split(/(\\|\/)/g).pop(),
            fileItem = $this.siblings('.file-item'),
            beginSlice = fileName.lastIndexOf('.') + 1,
            typeFile = fileName.slice(beginSlice);
      
          fileItem.find('.file-name').text(fileName);
          if (valText != '') {
            fileItem.removeClass('hide-btn');
      
            for (var i = 0; i < coutnTypeFiles; i++) {
      
              if (typeFile == typeFileArea[i]) {
                $this.parent().addClass('has-mach');
              }
            }
          } else {
            fileItem.addClass('hide-btn');
          }
      
          if (!$this.parent().hasClass('has-mach')) {
            $this.parent().addClass('error');
          }
        });
      
        //remove file
        $body.on('click', '.btn-del-file', function() {
          var elem = $(this).closest('.one-file');
          elem.fadeOut(400);
          setTimeout(function() {
            elem.remove();
          }, 400);
        });
      });
      input {
        display: none;
      }
      
      .files-wr {
        padding: 20px;
      }
      
      .files-wr label {
        margin-bottom: 20px;
        border-bottom: 1px dashed #177cca;
        position: relative;
        display: inline-block;
        color: #177cca;
        font-size: 18px;
        font-weight: 400;
        cursor: pointer;
        transition: all .2s;
      }
      
      .files-wr label:after {
        content: '+';
        width: 32px;
        height: 32px;
        border-radius: 5px;
        background-color: #177cca;
        position: absolute;
        top: -4px;
        right: -47px;
        font-size: 18px;
        line-height: 32px;
        color: #fff;
        text-align: center;
        transition: all .2s;
      }
      
      .files-wr label:hover,
      .files-wr label:active {
        color: #77c639;
        border-color: #77c639;
      }
      
      .files-wr label:hover:after,
      .files-wr label:active:after {
        background-color: #77c639;
      }
      
      .files-wr .one-file~.one-file label {
        display: none;
      }
      
      .files-wr .one-file.error {
        border: none;
      }
      
      .files-wr .one-file.error .file-name {
        color: #ca4a17;
      }
      
      .files-wr .file-item {
        position: relative;
        margin-top: 4px;
        display: flex;
        align-items: center;
      }
      
      .files-wr .file-item.hide-btn {
        display: none;
      }
      
      .files-wr .file-name {
        font-size: 16px;
        font-style: italic;
        line-height: 26px;
        white-space: nowrap;
        overflow: hidden;
        text-overflow: ellipsis;
        padding-right: 10px;
      }
      
      .files-wr .btn-del-file {
        margin-left: 5px;
        width: 16px;
        min-width: 16px;
        height: 16px;
        line-height: 16px;
        border-radius: 3px;
        color: #fff;
        text-align: center;
        cursor: pointer;
        transition: all .2s;
        background-color: #177cca;
      }
      
      .files-wr .btn-del-file:hover,
      .files-wr .btn-del-file:focus {
        background-color: #ca4a17;
      }
      <script src="https://ajax.googleapis.com/ajax/libs/jquery/2.1.1/jquery.min.js"></script>
      <div class="files-wr" data-count-files="1">
        <div class="one-file">
          <label for="file-1">Attach file</label>
          <input name="file-1" id="file-1" type="file">
          <div class="file-item hide-btn">
            <span class="file-name"></span>
            <span class="btn btn-del-file">x</span>
          </div>
        </div>
      </div>

      【讨论】:

        猜你喜欢
        • 2020-11-01
        • 2019-04-28
        • 1970-01-01
        • 1970-01-01
        • 1970-01-01
        • 1970-01-01
        • 1970-01-01
        • 2020-01-09
        • 1970-01-01
        相关资源
        最近更新 更多