简体   繁体   English

在现代浏览器中删除文件

[英]Removing a file in modern browsers

Problem 问题

I am currently using ( https://github.com/blueimp/jQuery-File-Upload/wiki ) this jQuery HTML5 Uploader. 我目前正在使用( https://github.com/blueimp/jQuery-File-Upload/wiki )这个jQuery HTML5 Uploader。 The basic version, no ui. 基本版,没有用户。

The big problem is, that I looked everywhere (Mozilla Developer Network, SO, Google, etc.) and found no solution for removing a files already added via dragNdrop or manually via the file input dialogue. 最大的问题是,我到处寻找(Mozilla开发者网络,SO,谷歌等)并找不到删除已经通过dragNdrop添加的文件或通过文件输入对话手动添加的文件的解决方案。

Why do I want to achieve removing a file? 为什么我要删除文件? Because it seems that HTML5 has a kind of "bug". 因为HTML5似乎有一种“bug”。 If you drop / select a file (file input has set multiple) upload it, and then drop / select another file you magically have now the new file twice and it gets uploaded twice. 如果您删除/选择一个文件(文件输入已设置多个)上传它,然后删除/选择另一个文件,你现在神奇地拥有两个新文件,并上传两次。

To prevent this magic file caching the use would have to refresh the page, which is not what someone wants to have for his modern AJAX web app. 为了防止这个神奇的文件缓存,使用必须刷新页面,这不是某人想要的现代AJAX Web应用程序。

What I have tried so far: 到目前为止我尝试过的:

  • .reset() 。重启()
  • .remove() 。去掉()
  • Reset Button 复位按钮
  • Setting .val() to '' 将.val()设置为''

This seems to be a general HTML5 JS problem not jQuery specific. 这似乎是一个普通的HTML5 JS问题,而不是jQuery特有的。

Theory 理论

Might it be, that $j('#post').click (I bind / re-bind a lot of times different callbacks), stacks the callbacks methods so that each time the updateFileupload function is called an additional callback is set. 可能是, $j('#post').click (我绑定/重新绑定很多次不同的回调),堆栈回调方法,这样每次调用updateFileupload函数时都会设置一个额外的回调。 The actual problem would now not rely anymore on the HTML5 upload, it would now rely on my could, miss-binding the .click action on my submit button (id=#post). 实际问题现在不再依赖于HTML5上传,它现在依赖于我的能力,错过绑定我的提交按钮上的.click动作(id = #post)。 If we now call .unbind before each .click there shouldn't be any duplicated callback binding. 如果我们现在在每个.click之前调用.unbind,那么就不应该有任何重复的回调绑定。

Code

Function containing the upload code: 包含上传代码的函数:

function updateFileupload (type) {
            var destination = "";

            switch(type)
            {
                case upload_type.file:
                    destination = '/wall/uploadfile/id/<?=$this->id?>';
                    break;
                case upload_type.image:
                    destination = '/wall/upload/id/<?=$this->id?>';
                    break;
            }

            $j('#fileupload').fileupload({
                dataType: 'json',
                url: destination,
                singleFileUploads: false,
                autoUpload: false,
                dropZone: $k(".dropZone"),
                done: function (e, data) {
                    console.log("--:--");
                    console.log(data.result);
                    upload_result = data.result;
                    console.log(upload_result);
                    console.log("--:--");
                    console.log(type);
                    if(type == upload_type.image)
                    {
                        var imageName = upload_result.real;
                                        console.log(imageName);
                                        $k.get('/wall/addpicture/id/<?=$this->id ?>/name'+imageName, function(data){
                                                if(data > 0){
                                                        console.log("I made it through!");
                                                        if(!data.id)
                                                        {
                                                            $k('#imgUpload').html(''); 
                                                            //$k('#imgPreview').fadeOut(); 
                                                            $k('#newPost').val('');
                                                            $k.get("/wall/entry/id/"+data, function(html){
                                                                    $k('#postList').prepend(html);

                                                            }); 
                                                        }
                                                }
                                        });
                    }
                  },
                send: function(e, data){

                        var files = data.files;
                        var duplicates = Array(); // Iterate over all entries and check whether any entry matches the current and add it to duplicates for deletion

                        for(var i=0; i<data.files.length;i++)
                        {
                            for(var j=0;j<data.files.length-1;j++)
                            {
                                if(files[i].name == files[j].name && i != j)
                                {
                                    duplicates.push(j);
                                }
                            }

                        }
                        if(duplicates.length > 0)
                        {
                            for(var i=0;i<duplicates.length;i++)
                                files.splice(i, 1);
                        }

                        console.log("Duplicates");
                        console.log(duplicates);
                },
                drop: function(e, data){
                    console.log("outside");
//                    $k.each(data.files, function(index, file){
//                            $k('#imageListDummy').after('<li class="file-small-info-box">'+file.name+'</li>');
//                            console.log(file);
//                            
//                        });
                },
                add: function(e, data){

                        upload_data = data;

                        console.log(data);
                        $k.each(data.files, function(index, file){
                            $k('#imageListDummy').after('<li class="file-small-info-box">'+file.name+'</li>');
                            console.log(file);

                        });
                        $j('#post').click(function(event){
                                    upload_data.submit();

                                    if(type == upload_type.image)
                                    {
                                        var file = upload_data.files[0];
                                        console.log("I am here");
                                        console.log(file);
                                        var img = document.createElement("img");


                                        img.src = window.URL.createObjectURL(file);
                                        img.height = 64;
                                        img.width = 64;
                                        img.onload = function(e) {  
                                            window.URL.revokeObjectURL(this.src);  
                                        }
                                        document.getElementById('imgPreview').appendChild(img);


                                        $k('#imgPreview').show();
                                    }
                                    clickPostCallback(event);


                                   });
                        $j('#showSubmit').show();
                    }

                });
        }

It could be more a browser security issue. 它可能更像是一个浏览器安全问题。 Current file uploads specs don't allow javascript (or anything as far as I know) to tamper with the value of the file field even if to remove it. 当前文件上传规范不允许javascript(或我所知的任何东西)篡改文件字段的值,即使要删除它。

So I would imagine any good file uploader would create multiple file upload fields so you can remove the entire field rather than play with the value? 所以我认为任何好的文件上传器都会创建多个文件上传字段,这样你就可以删除整个字段而不是使用值?

This is speculation though. 这是猜测。

Updated answer to Updated Question: 已更新问题的更新答案:

Shouldn't click() only be bound once? 不应该点击()只绑定一次? you shouldn't need to rebind a click event to a single element '#post' (unless this element changes, in which case it should really be a class). 你不需要将click事件重新绑定到单个元素'#post'(除非这个元素改变,在这种情况下它应该是一个类)。 You can place the click() event binding outside of the options for file upload, as long as it's contained in a $(function(){} so it's when the DOM's ready. 你可以将click()事件绑定放在文件上传选项之外,只要它包含在$(function(){}中,所以当DOM准备就绪时。

Aside from that I'm trying to read the code without any HTML and no experience in multiple file uploading. 除此之外,我正在尝试阅读没有任何HTML的代码,也没有多文件上传的经验。 The best thing to do is try and re-create it on jsfiddle.net , that way others can go in and play around with the code without affecting you and your likely to find the problem while putting the code in there anyway :) 最好的办法是尝试在jsfiddle.net上重新创建它,这样其他人可以进入并使用代码而不会影响你,并且你可能会在将代码放在那里时发现问题:)

声明:本站的技术帖子网页,遵循CC BY-SA 4.0协议,如果您需要转载,请注明本站网址或者原文地址。任何问题请咨询:yoyou2525@163.com.

 
粤ICP备18138465号  © 2020-2024 STACKOOM.COM