Download jquery file - how to rewrite NOT rename file

I am trying to update a jQuery download plugin, so when you upload a file, it just overwrites the existing file with the same name, rather than renaming it with upcount.

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

but I can not rewrite this plugin to make it work?

there is already an open question that has not yet been answered here: jQuery Uploading a file using bluimp, how to replace instead of renaming

Any guidance on this would be greatly appreciated.

+8
source share
8 answers

If you use UploadHandler.php, which exists on 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); 
+8
source

2016 UPDATE MKoosej 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); 
+4
source

I needed an option to overwrite files.

Using the PHP file UploadHandler.php, I made a very quick hack that works for me. I am sure there is a better way, but it can help others.

Look at the 'upcount_name_callback' handler and replace the return statement with the numbering (index). I left the original return statement in case I need to return :)

 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; */ } 
+2
source

Review the github documentation ( here ) and scroll down to where it says “ How to link a file to a node element during the download life cycle ”. 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 callback function made . If you expand the data strong> object , you will see that there is a data.files object, as well as a data.result object. data.result contains the file name on the server . Therefore, the name of the recently uploaded file on the server data.result.files [0] .name is based on the above script example.

I hope this helps someone

0
source

I tried @MKoosej's solution. In my case, this worked fine, but I also edited trim_file_name , where I force it to return only one file name (I had to make a bootloader that converts any file name to data.csv). I also had to make a different version of the bootloader, where I should rewrite the file name instead of renaming, but different files with different names should remain different. Therefore, when I tried the @MKoosej solution there, it started to download the file only with the extension, for example .jpeg, without a file name. So, I found this solution: in func get_unique_filename at the very beginning I said the following:

 $oldName=$name; 

and returned $oldName instead of $name , and everything was fine.

0
source

Anno 2018 this might work too (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); } } 

This avoids the need to modify the source code, which can then be used as a library.

0
source

Set one name and overwrite the file without problems. But there is a big problem in another. Namely, in the identification of the file when downloading parts.

Check out this situation. 1. You have replaced the return $ this → get_unique_filename method, and now it returns the same name. Good. 2. You download a large file in parts. Good. What will happen if the download stops due to poor internet? 3. When you start downloading the same file a second time, you will not succeed. How will he try to add this to something that has not been fully loaded. Very bad. 4. I also want to say that if you upload a completely different file, you can’t do anything, because it will be added to the end of the one that was not added again.

Conclusion: you need to add the identification of each file and the flag of the new download to the plugin, or add the old file.

0
source

Just execute the ajax request in the callback to load and delete the file from the data disk. You can find the name of the data file data.files [0] .name.

Then you just look for tr with the old link, for example:

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

and disappear.

Hi

-2
source

All Articles