Drupal duplicate filename throws AJAX error
As per Drupal structure you need to add option "FILE_EXISTS_RENAME" during save file for example :
 $file_temp = file_save_data($attachment->data, 'public:///' , FILE_EXISTS_RENAME);