How to download from private// following a link? I have a custom entity (contract) created in a custom module (amap) by a bunch of BaseFieldDefinition, one of which being  

$fields['file1'] = BaseFieldDefinition::create('file')
  -&gt;setLabel(t('Source file'))
  -&gt;setDescription(t('Source file (may be modified)'))
  -&gt;setSettings([
    'target_type' =&gt; 'file',
    'file_extensions' =&gt; 'doc docx odt pages',
    'uri_scheme' =&gt; 'private',
    'file_directory' =&gt; 'contracts',
  ])


I have a View about the custom entity that displays some fields in table mode:

The download action (the yellow arrow) is defined using Custom text with rewrite results, link path being {{ file1__target_id }}:
The link is something like mysite/system/files?file=contracts/Contract-4-20200408.doc (for example).
And following the link gives different results depending on the browser. None of them being what I want: the default filename proposed is not correct 'files' or 'file.dot' instead of 'Contract-4-20200408.doc' (in my example).  

So I tried to implement hook_file_download. My code is  

function amap_file_download($uri) {
  $sFileName =  \Drupal::service('file_system')-&gt;basename($uri);
  $sFileNameWithPath = 'sites/default/files/_private/contracts/' . $sFileName;
  $response = new BinaryFileResponse($sFileNameWithPath);
  $response-&gt;setContentDisposition(ResponseHeaderBag::DISPOSITION_ATTACHMENT, $sFileName);
  return $response;
}


and there I get this error!  

Notice: Object of class Symfony\Component\HttpFoundation\BinaryFileResponse could not be converted to int in Drupal\system\FileDownloadController-&gt;download() (line 51 of core/modules/system/src/FileDownloadController.php).
Drupal\system\FileDownloadController-&gt;download(Object, 'private')
call_user_func_array(Array, Array) (Line: 123)
Drupal\Core\EventSubscriber\EarlyRenderingControllerWrapperSubscriber-&gt;Drupal\Core\EventSubscriber\{closure}() (Line: 582)
Drupal\Core\Render\Renderer-&gt;executeInRenderContext(Object, Object) (Line: 124)
Drupal\Core\EventSubscriber\EarlyRenderingControllerWrapperSubscriber-&gt;wrapControllerExecutionInRenderContext(Array, Array) (Line: 97)
Drupal\Core\EventSubscriber\EarlyRenderingControllerWrapperSubscriber-&gt;Drupal\Core\EventSubscriber\{closure}() (Line: 151)
Symfony\Component\HttpKernel\HttpKernel-&gt;handleRaw(Object, 1) (Line: 68)
Symfony\Component\HttpKernel\HttpKernel-&gt;handle(Object, 1, 1) (Line: 57)
Drupal\Core\StackMiddleware\Session-&gt;handle(Object, 1, 1) (Line: 47)
Drupal\Core\StackMiddleware\KernelPreHandle-&gt;handle(Object, 1, 1) (Line: 99)
Drupal\page_cache\StackMiddleware\PageCache-&gt;pass(Object, 1, 1) (Line: 78)
Drupal\page_cache\StackMiddleware\PageCache-&gt;handle(Object, 1, 1) (Line: 47)
Drupal\Core\StackMiddleware\ReverseProxyMiddleware-&gt;handle(Object, 1, 1) (Line: 52)
Drupal\Core\StackMiddleware\NegotiationMiddleware-&gt;handle(Object, 1, 1) (Line: 23)
Stack\StackedHttpKernel-&gt;handle(Object, 1, 1) (Line: 693)
Drupal\Core\DrupalKernel-&gt;handle(Object) (Line: 19)


What am I doing wrong?
Thanks to @Kevin, I updated my code to

function amap_file_download($uri) {

  $sFileName =  \Drupal::service('file_system')-&gt;basename($uri);
  return array(
      'Content-disposition' =&gt; 'attachment; filename="' . $sFileName . '"',
  );

}


And it works!