问题
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.
回答1:
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);
回答2:
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);
回答3:
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; */
}
回答4:
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.
回答5:
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
回答6:
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.
回答7:
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
来源:https://stackoverflow.com/questions/14787055/jquery-file-upload-how-to-overwrite-file-not-rename