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 无线电能传输系统MATLAB仿真问题
  • ¥50 如何用脚本实现输入法的热键设置
  • ¥20 我想使用一些网络协议或者部分协议也行,主要想实现类似于traceroute的一定步长内的路由拓扑功能
  • ¥30 深度学习,前后端连接
  • ¥15 孟德尔随机化结果不一致
  • ¥15 apm2.8飞控罗盘bad health,加速度计校准失败
  • ¥15 求解O-S方程的特征值问题给出边界层布拉休斯平行流的中性曲线
  • ¥15 谁有desed数据集呀
  • ¥20 手写数字识别运行c仿真时,程序报错错误代码sim211-100
  • ¥15 关于#hadoop#的问题