jquery file upload - how to overwrite file NOT rename
Asked Answered
F

7

8

I am struggling to update the Jquery file upload plugin so when you upload a file it just overwrites an existing file with the same name instead of renaming it with an upcount.

I have applied the change covered in this link: https://github.com/blueimp/jQuery-File-Upload/issues/1965

but I can't seem overwrite this plugin to get this working?

there's an existing open question not yet answered here: jQuery File Upload by bluimp, how to replace instead of renaming

any guidance on this would be much appreciated.

Franci answered 9/2, 2013 at 10:17 Comment(2)
Did you maybe find a solution for overwriting a file? I need it too. ThanksChirpy
have a look here : https://mcmap.net/q/1326237/-jquery-file-upload-by-bluimp-how-to-replace-instead-of-renamingChatham
B
8

If you using UploadHandler.php that is exists in the wiki it's simple. In the get_file_name method you should replace this line:

    return $this -> get_unique_filename($this -> trim_file_name($name, $type, $index, $content_range), $type, $index, $content_range);

with this:

 return $this -> trim_file_name($name, $type, $index, $content_range);
Blowgun answered 11/7, 2013 at 18:17 Comment(1)
Lol, man, your solution helped me, but actually this didn't work in my case. I'll write my own answer then.Lumbye
K
4

2016 UPDATE to MKoosej's fix! In UploadHandler.php → protected function get_file_name ... replace

return $this->get_unique_filename(
        $file_path,
        $this->fix_file_extension($file_path, $name, $size, $type, $error,
            $index, $content_range),
        $size,
        $type,
        $error,
        $index,
        $content_range
    );

with

return $this -> trim_file_name($file_path, $name, $size, $type, $error, $index, $content_range);
Kendo answered 1/4, 2016 at 14:10 Comment(1)
The other solutions no longer work with the new version (2016+) of Upload. This one does.Giltedged
L
2

I needed the same file overwrite option.

Using the PHP UploadHandler.php file I did a really quick hack which works for me. I'm sure there is a better way, but this may help others.

Look for handler 'upcount_name_callback' and replace the return statement with the (index) numbering taken out. I left the original return statement in case I need to go back :)

protected function upcount_name_callback($matches) {
    $index = isset($matches[1]) ? intval($matches[1]) + 1 : 1;
    $ext = isset($matches[2]) ? $matches[2] : '';
    return ''.$ext;
    /* return ' ('.$index.')'.$ext; */
}
Laritalariviere answered 28/6, 2013 at 0:33 Comment(0)
M
1

Anno 2018 this could work as well (PHP 7.1):

class MyUploadHandler extends UploadHandler
{
    public function __construct($options = array(), $initialize = true, $error_messages = null) {
        $options += array(
            'overwrite_existing' => true
        );

        parent::__construct($options, $initialize, $error_messages);
    }

    protected function get_unique_filename($file_path, $name, $size, $type, $error, $index, $content_range) {
        if ($this->options['overwrite_existing'] ?? false) {
            return $name;
        }

        return parent::get_unique_filename($file_path, $name, $size, $type, $error, $index, $content_range);
    }
}

It avoids having to change the original code, which then can be used as a library.

Markowitz answered 11/7, 2018 at 10:12 Comment(0)
S
0

Consider the documentation on github (here) and scroll down to where it says "How to tie a file to an element node during the life cycle of an upload". This is the code:

$(function () {
    $('#fileupload').fileupload({
        dataType: 'json',
        add: function (e, data) {
            data.context = $('<p/>').text('Uploading...').appendTo(document.body);
            data.submit();
        },
        done: function (e, data) {
            data.context.text('Upload finished.');
        }
    });
});

Pay attention to the done callback function. If you expand the data object you will note there is a data.files object as well as a data.result object. data.result contains the name of the file on the server. Hence, the name of newly uploaded file on the server is data.result.files[0].name based on the example script given above.

I hope this helps someone

Squatter answered 21/7, 2013 at 6:12 Comment(0)
L
0

I tried solution of @MKoosej. In my case it worked fine, but I've also edited trim_file_name where I make it to return only one filename (I had to do an uploader which transform any filename to data.csv). Also I had to make other version of uploader where I should rewrite filename instead of rename but the different files with different names should remain different. So when I tried there @MKoosej solution it started to upload file with only extension like .jpeg without filename. So, I found this solution: in func get_unique_filename in the very beginning I put this:

$oldName=$name;

and returned $oldName instead of $name and it was fine.

Lumbye answered 24/6, 2015 at 10:38 Comment(0)
H
-2

Just do a ajax request within the callback for starting upload and erase the file from the data disk. You can find the filename withing data.files[0].name.

Then you just search for the tr with the old link, like this:

$("a[title='"+ data.files[0].name + "']").each(function () {
        $(this).closest('tr').fadeOut();
    });

and fade it out.

Regards

Hypo answered 8/5, 2013 at 7:22 Comment(1)
i'm sorry, but i can't figure out how a DOM operation on a table row has anything to do with the question at hand, or even your suggestion of deleting the file before the upload...Lungworm

© 2022 - 2024 — McMap. All rights reserved.