Ajax add command to open a new URL in a custom module, I created a custom form. On this form, I have an ajax button to open a specific file (.pdf).

Here is my code in a custom submit function :

  $url = 'myfileUrl';
  $response = new AjaxResponse();
  $command = new RedirectCommand($url);
  $response-&gt;addCommand($command);


It's ok and the file is opened when I click on the button. But, the page is closed to open the file. How can I specify an attribute "target" to open the file in a new window? If not possible, is there another ajax command to open an external URL?

Thanks.
I found a solution to resolve my problem.
After having readed several posts, i have decided to create a custom Ajax Command to open my files (pdf/csv).

Create a custom command - link

In my custom module, I have built the required files for this and call in my CallBack function this new ajax command ('OpenFileCommand') :

    $ajax_response = new AjaxResponse();
    $command = new OpenFileCommand($file_url);
    $ajax_response-&gt;addCommand($command);


Here is the simple code for this command:

use Drupal\Core\Ajax\CommandInterface;

/**
 * Class OpenFileCommand.
 */
class OpenFileCommand implements CommandInterface {

  // Create a new property.    
  protected $file_url;

  // Construct the File Url property.
  public function __construct($file_url) {
    $this-&gt;file_url = $file_url;
  }

  /**
  * Render custom ajax command.
  *
  * @return ajax command function
  */
  public function render() {
    return [
      'command' =&gt; 'OpenFileCommand',
      'file_url' =&gt; $this-&gt;file_url
    ];
  }

}


Now, I can open a file when I click on a ajax button.

Thanks for all for your help. This solution can help someone...