Using wp_handle_upload and media_handle_sideload with ajax

Uploading files in ajax is a bit tricky because it is not possible to upload files using the browser’s XMLHttpRequest object so you need to use some kind of Ajax upload plugin.

Also wp_handle_upload() is not what your using in your code its media_handle_sideload()

  • wp_handle_upload() – should be used for file uploads (input file field)
  • media_handle_sideload() – should be used for remote file uploads
    (input text field)

take a look at this similar question