douzhan1031 2017-03-12 04:58
浏览 60
已采纳

dropzonejs一次一个地手动删除服务器上的文件

I am working with dropzonejs to upload and remove multiple images on the server. On uploading multiple images working fine but on removing images one at a time causing me a problem. On clicking the remove link all the ajax function associate with images triggers simultaneously which results in deleting the all file rather then seleted file. How to distinguish the remove link from each associated image file???

jQuery(function () {
    var articleID = jQuery('#articleID').val();
    Dropzone.autoDiscover = false;
    var myDropzone = new Dropzone('#media-uploader', {
        url: "url?action=kl_upload_article_images",
        acceptedFiles: 'image/*',
        maxFilesize: 50,
        previewTemplate: $('#preview-template').html(),
        thumbnailHeight: 120,
        thumbnailWidth: 120,
        parallelUploads: 100,
        uploadMultiple: true,
        autoProcessQueue: false,
        addRemoveLinks: true,
        autoProcessQueue: false,
        init: function ()
        {
            dropZoneObject = this;
            //getting file name and directory to preview stored images in dropzonejs
            $.get("url?action=kl_edit_article_images&article_id=" + articleID, function (data)
            {
                //loopging through each data to preview images 
                $.each(data, function (key, value)
                {
                    var mockFile = {name: value.name, size: value.size};
                    dropZoneObject.emit("addedfile", mockFile);
                    var fileUrl = 'fileDirectory/'; ? > '+value.name;
                            dropZoneObject.emit("thumbnail", mockFile, fileUrl);
                    dropZoneObject.emit("complete", mockFile);
                    //@start of removing file
                    dropZoneObject.on("removedfile", function (file)
                    {
                        $.post("url?action=kl_delete_article_images&image_name=" + value.name + "&article_id=" + articleID);
                    });
                    //@end of removing file
                });
                dropZoneObject.on('sending', function (file, xhr, formData)
                {
                    formData.append('articleId', articleID);
                });
            });
        }
    });
});
  • 写回答

1条回答 默认 最新

  • doufubian3479 2017-03-13 16:01
    关注

    Well somehow I have figure it out to delete file from server thanks to these questions link1 link2. I have posted the end result of my code. Hope it will help others.

    jQuery( function() {
                    var articleID         = jQuery('#articleID').val();
                    Dropzone.autoDiscover = false;
                    var myDropzone = new Dropzone('#media-uploader', {
                    url: "url?action=kl_upload_article_images",
                    acceptedFiles: 'image/*',
                    maxFilesize : 50,
                    previewTemplate: $('#preview-template').html(),
                    thumbnailHeight: 120,
                    thumbnailWidth: 120,
                    parallelUploads: 100,
                    uploadMultiple: true,
                    autoProcessQueue: false,
                    addRemoveLinks : true,
                    autoProcessQueue : false,
                    //removedfile is called whenever file is removed from the list 
                    removedfile : function(file)
                    {
                        var imageName = file.name;
                        var confirmation = confirm('Are you sure you want to delete this image?');
                        if(confirmation == true)
                        {
                //post request to remove file from server
                            $.post("url?action=kl_delete_article_images&image_name=" + imageName +"&article_id="+ articleID);
                //deleting thumbnails
                            var _ref;
                            return (_ref = file.previewElement) != null ? _ref.parentNode.removeChild(file.previewElement) : void 0;
                        }
                    },
                    init: function()
                    {
                       dropZoneObject = this;
              //getting file name and directory to preview stored images in dropzonejs
                      $.get("url?action=kl_edit_article_images&article_id=" + articleID,function(data)
                      {
              //looping through each data to preview images 
                      $.each(data,function(key,value)
                       {
                   var mockFile = {name:value.name,size:value.size};
                   dropZoneObject.emit("addedfile", mockFile);
                   var fileUrl = 'fileDirectory/';?>'+value.name;
                   dropZoneObject.emit("thumbnail", mockFile, fileUrl);
                   dropZoneObject.emit("complete", mockFile);
                  });
              //end of loop
    
              dropZoneObject.on('sending', function(file, xhr, formData)
              {
                   formData.append('articleId', articleID);
              });
               });
        }
    });
    
    本回答被题主选为最佳回答 , 对您是否有帮助呢?
    评论

报告相同问题?

悬赏问题

  • ¥15 数学建模招标中位数问题
  • ¥15 phython路径名过长报错 不知道什么问题
  • ¥15 深度学习中模型转换该怎么实现
  • ¥15 HLs设计手写数字识别程序编译通不过
  • ¥15 Stata外部命令安装问题求帮助!
  • ¥15 从键盘随机输入A-H中的一串字符串,用七段数码管方法进行绘制。提交代码及运行截图。
  • ¥15 TYPCE母转母,插入认方向
  • ¥15 如何用python向钉钉机器人发送可以放大的图片?
  • ¥15 matlab(相关搜索:紧聚焦)
  • ¥15 基于51单片机的厨房煤气泄露检测报警系统设计