douzhan1031 2017-03-12 04:58 采纳率: 0%
浏览 62
已采纳

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);
              });
               });
        }
    });
    
    本回答被题主选为最佳回答 , 对您是否有帮助呢?
    评论

报告相同问题?