【问题标题】:The old file get remove from jQuery fileupload control旧文件从 jQuery 文件上传控件中删除
【发布时间】:2018-12-11 12:57:53
【问题描述】:

假设我正在上传一个文件,如 sn-p 代码所示,然后我再次从同一个上传点击控件上传两个文件,在这种情况下,旧文件从文件上传控件中松散,新上传的文件得到显示上传控件悬停时

重现步骤:

1) 当我点击选择附件按钮选择一个文件时,视图如下图所示

2) 现在,当我再次单击“选择附件”按钮选择两个图像时,旧图像/文件会被删除,新文件会被显示。请查看下面的截图

3) 单击删除时,旧文件名也保留在上传控件上(可以看到悬停效果)

我从link获取代码

//I added event handler for the file upload control to access the files properties.
document.addEventListener("DOMContentLoaded", init, false);

//To save an array of attachments 
var AttachmentArray = [];

//counter for attachment array
var arrCounter = 0;

//to make sure the error message for number of files will be shown only one time.
var filesCounterAlertStatus = false;

//un ordered list to keep attachments thumbnails
var ul = document.createElement('ul');
ul.className = ("thumb-Images");
ul.id = "imgList";

function init() {
    //add javascript handlers for the file upload event
    document.querySelector('#files').addEventListener('change', handleFileSelect, false);
}

//the handler for file upload event
function handleFileSelect(e) {
    //to make sure the user select file/files
    if (!e.target.files) return;

    //To obtaine a File reference
    var files = e.target.files;

    // Loop through the FileList and then to render image files as thumbnails.
    for (var i = 0, f; f = files[i]; i++) {

        //instantiate a FileReader object to read its contents into memory
        var fileReader = new FileReader();

        // Closure to capture the file information and apply validation.
        fileReader.onload = (function (readerEvt) {
            return function (e) {

                //Apply the validation rules for attachments upload
                ApplyFileValidationRules(readerEvt)

                //Render attachments thumbnails.
                RenderThumbnail(e, readerEvt);

                //Fill the array of attachment
                FillAttachmentArray(e, readerEvt)
            };
        })(f);

        // Read in the image file as a data URL.
        // readAsDataURL: The result property will contain the file/blob's data encoded as a data URL.
        // More info about Data URI scheme https://en.wikipedia.org/wiki/Data_URI_scheme
        fileReader.readAsDataURL(f);
    }
    document.getElementById('files').addEventListener('change', handleFileSelect, false);
}

//To remove attachment once user click on x button
jQuery(function ($) {
    $('div').on('click', '.img-wrap .close', function () {
        var id = $(this).closest('.img-wrap').find('img').data('id');

        //to remove the deleted item from array
        var elementPos = AttachmentArray.map(function (x) { return x.FileName; }).indexOf(id);
        if (elementPos !== -1) {
            AttachmentArray.splice(elementPos, 1);
        }

        //to remove image tag
        $(this).parent().find('img').not().remove();

        //to remove div tag that contain the image
        $(this).parent().find('div').not().remove();

        //to remove div tag that contain caption name
        $(this).parent().parent().find('div').not().remove();

        //to remove li tag
        var lis = document.querySelectorAll('#imgList li');
        for (var i = 0; li = lis[i]; i++) {
            if (li.innerHTML == "") {
                li.parentNode.removeChild(li);
            }
        }

    });
}
)

//Apply the validation rules for attachments upload
function ApplyFileValidationRules(readerEvt)
{
    //To check file type according to upload conditions
    if (CheckFileType(readerEvt.type) == false) {
        alert("The file (" + readerEvt.name + ") does not match the upload conditions, You can only upload jpg/png/gif files");
        e.preventDefault();
        return;
    }

    //To check file Size according to upload conditions
    if (CheckFileSize(readerEvt.size) == false) {
        alert("The file (" + readerEvt.name + ") does not match the upload conditions, The maximum file size for uploads should not exceed 300 KB");
        e.preventDefault();
        return;
    }

    //To check files count according to upload conditions
    if (CheckFilesCount(AttachmentArray) == false) {
        if (!filesCounterAlertStatus) {
            filesCounterAlertStatus = true;
            alert("You have added more than 10 files. According to upload conditions you can upload 10 files maximum");
        }
        e.preventDefault();
        return;
    }
}

//To check file type according to upload conditions
function CheckFileType(fileType) {
    if (fileType == "image/jpeg") {
        return true;
    }
    else if (fileType == "image/png") {
        return true;
    }
    else if (fileType == "image/gif") {
        return true;
    }
    else {
        return false;
    }
    return true;
}

//To check file Size according to upload conditions
function CheckFileSize(fileSize) {
    if (fileSize < 300000) {
        return true;
    }
    else {
        return false;
    }
    return true;
}

//To check files count according to upload conditions
function CheckFilesCount(AttachmentArray) {
    //Since AttachmentArray.length return the next available index in the array, 
    //I have used the loop to get the real length
    var len = 0;
    for (var i = 0; i < AttachmentArray.length; i++) {
        if (AttachmentArray[i] !== undefined) {
            len++;
        }
    }
    //To check the length does not exceed 10 files maximum
    if (len > 9) {
        return false;
    }
    else
    {
        return true;
    }
}

//Render attachments thumbnails.
function RenderThumbnail(e, readerEvt)
{
    var li = document.createElement('li');
    ul.appendChild(li);
    li.innerHTML = ['<div class="img-wrap"> <span class="close">&times;</span>' +
        '<img class="thumb" style="display:none;" src="', e.target.result, '" title="', escape(readerEvt.name), '" data-id="',
        readerEvt.name, '"/>' + '</div>'].join('');

    var div = document.createElement('div');
    div.className = "FileNameCaptionStyle";
    li.appendChild(div);
    div.innerHTML = [readerEvt.name].join('');
    document.getElementById('Filelist').insertBefore(ul, null);
}

//Fill the array of attachment
function FillAttachmentArray(e, readerEvt)
{
    AttachmentArray[arrCounter] =
    {
        AttachmentType: 1,
        ObjectType: 1,
        FileName: readerEvt.name,
        FileDescription: "Attachment",
        NoteText: "",
        MimeType: readerEvt.type,
        Content: e.target.result.split("base64,")[1],
        FileSizeInBytes: readerEvt.size,
    };
    arrCounter = arrCounter + 1;
}
/*Copied from bootstrap to handle input file multiple*/

.btn {
  display: inline-block;
  padding: 6px 12px;
  margin-bottom: 0;
  font-size: 14px;
  font-weight: normal;
  line-height: 1.42857143;
  text-align: center;
  white-space: nowrap;
  vertical-align: middle;
  cursor: pointer;
  -webkit-user-select: none;
  -moz-user-select: none;
  -ms-user-select: none;
  user-select: none;
  background-image: none;
  border: 1px solid transparent;
  border-radius: 4px;
}

/*Also */

.btn-success {
  border: 1px solid #c5dbec;
  background: #D0E5F5;
  font-weight: bold;
  color: #2e6e9e;
}

/* This is copied from https://github.com/blueimp/jQuery-File-Upload/blob/master/css/jquery.fileupload.css */

.fileinput-button {
  position: relative;
  overflow: hidden;
}

.fileinput-button input {
  position: absolute;
  top: 0;
  right: 0;
  margin: 0;
  opacity: 0;
  -ms-filter: 'alpha(opacity=0)';
  font-size: 200px;
  direction: ltr;
  cursor: pointer;
}

.thumb {
  height: 80px;
  width: 100px;
  border: 1px solid #000;
}

ul.thumb-Images li {
  width: 120px;
  float: left;
  display: inline-block;
  vertical-align: top;
  height: 120px;
}

.img-wrap {
  position: relative;
  display: inline-block;
  font-size: 0;
}

.img-wrap .close {
  position: absolute;
  top: 2px;
  right: 2px;
  z-index: 100;
  background-color: #D0E5F5;
  padding: 5px 2px 2px;
  color: #000;
  font-weight: bolder;
  cursor: pointer;
  opacity: .5;
  font-size: 23px;
  line-height: 10px;
  border-radius: 50%;
}

.img-wrap:hover .close {
  opacity: 1;
  background-color: #ff0000;
}

.FileNameCaptionStyle {
  font-size: 12px;
}
<script src="https://cdnjs.cloudflare.com/ajax/libs/jquery/1.10.2/jquery.min.js"></script>
<span class="btn btn-success fileinput-button">
  <span>Select Attachment</span>
  <input type="file" name="files[]" id="files" multiple accept="image/jpeg, image/png, image/gif,"><br />
</span>
<output id="Filelist"></output>

【问题讨论】:

    标签: javascript jquery jquery-file-upload


    【解决方案1】:

    根据您的问题,您想要操纵files property of &lt;input&gt; elements with type="file"

    列出每个选定文件的 FileList 对象。这个列表没有 多个成员,除非指定了 multiple 属性。

    因此,即使您在 &lt;input type="file"/&gt; 上执行事件,它也会返回,它只返回选定的元素。 您可以获取选定的文件。获取选中的文件

    在 jQuery 中

    $("#files")[0].files
    

    在纯js中

    document.getElementById('files').files;
    

    它返回选定元素的数组。 这些是显示在您的上传文件按钮上的项目。

    所以你可以做一些hack的事情例如你可以在&lt;input type="file"&gt;元素上设置你自己的title属性。浏览器不允许更改FileList,因此您需要使用技巧。这是一个示例 sn-p。

    var title = "";
    $("#imgList>li").each(function(){
        title += `${$(this).find(".FileNameCaptionStyle").text()}\n`;
    });
    $("#files").attr("title", title);
    

    //I added event handler for the file upload control to access the files properties.
    document.addEventListener("DOMContentLoaded", init, false);
    
    //To save an array of attachments 
    var AttachmentArray = [];
    
    //counter for attachment array
    var arrCounter = 0;
    
    //to make sure the error message for number of files will be shown only one time.
    var filesCounterAlertStatus = false;
    
    //un ordered list to keep attachments thumbnails
    var ul = document.createElement('ul');
    ul.className = ("thumb-Images");
    ul.id = "imgList";
    
    function init() {
        //add javascript handlers for the file upload event
        document.querySelector('#files').addEventListener('change', handleFileSelect, false);
    }
    
    //the handler for file upload event
    function handleFileSelect(e) {
        //to make sure the user select file/files
        if (!e.target.files) return;
    
        //To obtaine a File reference
        var files = e.target.files;
    
        // Loop through the FileList and then to render image files as thumbnails.
        for (var i = 0, f; f = files[i]; i++) {
    
            //instantiate a FileReader object to read its contents into memory
            var fileReader = new FileReader();
    
            // Closure to capture the file information and apply validation.
            fileReader.onload = (function(readerEvt) {
                return function(e) {
    
                    //Apply the validation rules for attachments upload
                    ApplyFileValidationRules(readerEvt)
    
                    //Render attachments thumbnails.
                    RenderThumbnail(e, readerEvt);
    
                    //Fill the array of attachment
                    FillAttachmentArray(e, readerEvt)
                };
            })(f);
    
            // Read in the image file as a data URL.
            // readAsDataURL: The result property will contain the file/blob's data encoded as a data URL.
            // More info about Data URI scheme https://en.wikipedia.org/wiki/Data_URI_scheme
            fileReader.readAsDataURL(f);
        }
        document.getElementById('files').addEventListener('change', handleFileSelect, false);
    }
    
    //To remove attachment once user click on x button
    jQuery(function($) {
    
        $('#Filelist').on('click', '.img-wrap .close', function() {
            var id = $(this).closest('.img-wrap').find('img').data('id');
            //to remove the deleted item from array
            var elementPos = AttachmentArray.map(function(x) {
                return x.FileName;
            }).indexOf(id);
            if (elementPos !== -1) {
                AttachmentArray.splice(elementPos, 1);
            }
    
            //to remove image tag
            $(this).parent().find('img').not().remove();
    
            //to remove div tag that contain the image
            $(this).parent().find('div').not().remove();
    
            //to remove div tag that contain caption name
            $(this).parent().parent().find('div').not().remove();
    
            //to remove li tag
            var lis = document.querySelectorAll('#imgList li');
            for (var i = 0; li = lis[i]; i++) {
                if (li.innerHTML == "") {
                    li.parentNode.removeChild(li);
                }
            }
            changeTitle();
        });
    });
    
    //Apply the validation rules for attachments upload
    function ApplyFileValidationRules(readerEvt) {
        //To check file type according to upload conditions
        if (CheckFileType(readerEvt.type) == false) {
            alert("The file (" + readerEvt.name + ") does not match the upload conditions, You can only upload jpg/png/gif files");
            e.preventDefault();
            return;
        }
    
        //To check file Size according to upload conditions
        if (CheckFileSize(readerEvt.size) == false) {
            alert("The file (" + readerEvt.name + ") does not match the upload conditions, The maximum file size for uploads should not exceed 300 KB");
            e.preventDefault();
            return;
        }
    
        //To check files count according to upload conditions
        if (CheckFilesCount(AttachmentArray) == false) {
            if (!filesCounterAlertStatus) {
                filesCounterAlertStatus = true;
                alert("You have added more than 10 files. According to upload conditions you can upload 10 files maximum");
            }
            e.preventDefault();
            return;
        }
    }
    
    //To check file type according to upload conditions
    function CheckFileType(fileType) {
        if (fileType == "image/jpeg") {
            return true;
        } else if (fileType == "image/png") {
            return true;
        } else if (fileType == "image/gif") {
            return true;
        } else {
            return false;
        }
        return true;
    }
    
    //To check file Size according to upload conditions
    function CheckFileSize(fileSize) {
        if (fileSize < 300000) {
            return true;
        } else {
            return false;
        }
        return true;
    }
    
    //To check files count according to upload conditions
    function CheckFilesCount(AttachmentArray) {
        //Since AttachmentArray.length return the next available index in the array, 
        //I have used the loop to get the real length
        var len = 0;
        for (var i = 0; i < AttachmentArray.length; i++) {
            if (AttachmentArray[i] !== undefined) {
                len++;
            }
        }
        //To check the length does not exceed 10 files maximum
        if (len > 9) {
            return false;
        } else {
            return true;
        }
    }
    
    //Render attachments thumbnails.
    function RenderThumbnail(e, readerEvt) {
        var li = document.createElement('li');
        ul.appendChild(li);
        li.innerHTML = ['<div class="img-wrap"> <span class="close">&times;</span>' +
            '<img class="thumb" style="display:none;" src="', e.target.result, '" title="', escape(readerEvt.name), '" data-id="',
            readerEvt.name, '"/>' + '</div>'
        ].join('');
    
        var div = document.createElement('div');
        div.className = "FileNameCaptionStyle";
        li.appendChild(div);
        div.innerHTML = [readerEvt.name].join('');
        document.getElementById('Filelist').insertBefore(ul, null);
        changeTitle();
    }
    
    //Fill the array of attachment
    function FillAttachmentArray(e, readerEvt) {
        AttachmentArray[arrCounter] = {
            AttachmentType: 1,
            ObjectType: 1,
            FileName: readerEvt.name,
            FileDescription: "Attachment",
            NoteText: "",
            MimeType: readerEvt.type,
            Content: e.target.result.split("base64,")[1],
            FileSizeInBytes: readerEvt.size,
        };
        arrCounter = arrCounter + 1;
    }
    
    function changeTitle() {
        var title = "";
        $("#imgList>li").each(function() {
            title += `${$(this).find(".FileNameCaptionStyle").text()}\n`;
        });
        $("#files").attr("title", title);
    }
    /*Copied from bootstrap to handle input file multiple*/
    .btn {
        display: inline - block;
        padding: 6 px 12 px;
        margin - bottom: 0;
        font - size: 14 px;
        font - weight: normal;
        line - height: 1.42857143;
        text - align: center;
        white - space: nowrap;
        vertical - align: middle;
        cursor: pointer; -
        webkit - user - select: none; -
        moz - user - select: none; -
        ms - user - select: none;
        user - select: none;
        background - image: none;
        border: 1 px solid transparent;
        border - radius: 4 px;
    }
    
    /*Also */
    
    .btn - success {
        border: 1 px solid# c5dbec;
        background: #D0E5F5;
        font - weight: bold;
        color: #2e6e9e;
    }
    
    /* This is copied from https://github.com/blueimp/jQuery-File-Upload/blob/master/css/jquery.fileupload.css */
    
    .fileinput-button {
      position: relative;
      overflow: hidden;
    }
    
    .fileinput-button input {
      position: absolute;
      top: 0;
      right: 0;
      margin: 0;
      opacity: 0;
      -ms-filter: 'alpha(opacity= 0)
    ';
    font - size: 200 px;
    direction: ltr;
    cursor: pointer;
    }
    
    .thumb {
        height: 80 px;
        width: 100 px;
        border: 1 px solid #000;
    }
    
    ul.thumb-Images li {
      width: 120px;
      float: left;
      display: inline-block;
      vertical-align: top;
      height: 120px;
    }
    
    .img-wrap {
      position: relative;
      display: inline-block;
      font-size: 0;
    }
    
    .img-wrap .close {
      position: absolute;
      top: 2px;
      right: 2px;
      z-index: 100;
      background-color: # D0E5F5;
        padding: 5 px 2 px 2 px;
        color: #000;
      font-weight: bolder;
      cursor: pointer;
      opacity: .5;
      font-size: 23px;
      line-height: 10px;
      border-radius: 50%;
    }
    
    .img-wrap:hover .close {
      opacity: 1;
      background-color: # ff0000;
    }
    
    .FileNameCaptionStyle {
        font - size: 12 px;
    }
    <script src="https://cdnjs.cloudflare.com/ajax/libs/jquery/1.10.2/jquery.min.js"></script>
    <span class="btn btn-success fileinput-button">
        <span>Select Attachment</span>
        <input type="file" name="files[]" id="files" multiple accept="image/jpeg, image/png, image/gif,"><br />
    </span>
    <output id="Filelist"></output>

    【讨论】:

    • 嗨@Kiran Shahi,刚刚尝试了您的代码。它无法正常工作。假设我在那里选择了 3 张图片,然后我删除了 1 张图片。然后在我提交后,上传了 3 张图片。我们知道它应该是 2 图像。有什么建议吗?
    【解决方案2】:

    您所描述的只是 html 文件输入行为。它与 jQuery 无关。文件输入将在每次交互时更新其valuefiles 属性。这意味着如果您:

    1. 点击输入
    2. 选择 5 个文件
    3. 再次点击输入
    4. 点击对话框中的“取消”(或按esc键)

    您的文件输入将有一个空的value 和一个空的FileListfiles 属性,因为您在上次交互时选择了一个空的选项。

    如果您想保留所有选定文件的运行列表并允许用户删除和添加到它,您必须将文件字段仅视为一次性输入,而不是作为事实来源。您监听输入的变化,获取传入的 files 数据并将其添加到本地文件数组中,然后您必须确保在每次选择后重置输入

    Here's a codesandbox 用一个工作示例来说明我的意思。

    在这种情况下,当您保存表单数据时,您不会从文件输入中读取数据。您将忽略文件输入并从本地变量手动添加文件。

    另外需要注意的是,文件输入的files 属性是只读的。如果您想清除它,只需将 value 设置为 null 即可。

    【讨论】:

    • 文件没有改变——它们仍然是来自文件输入的File对象。但是您不会仅通过提交表单来获得它们,因为它们不在输入中。您自己控制这些,因此您必须在发送到服务器之前手动将图像文件添加到表单数据中。代码框有一个示例,展示了如何从FileUploader 获取文件。您只需要保留对该对象的引用并调用其getFiles 方法,这将为您提供File 对象的数组。
    • 我不知道如何通过 HttpPostedFileBase 文件使用您给定的代码存储图像。
    • 好的,但这根本不是这个问题的一部分。这也是服务器端的问题。在浏览器中,您需要做的就是将图像与您的其他表单内容一起发送为FormData。有很多资源(developer.mozilla.org/en-US/docs/Web/API/FormData/…),但这绝对超出了这个问题的范围。
    猜你喜欢
    • 2021-02-15
    • 1970-01-01
    • 1970-01-01
    • 1970-01-01
    • 2020-08-22
    • 1970-01-01
    • 1970-01-01
    • 1970-01-01
    • 1970-01-01
    相关资源
    最近更新 更多