Upload attachment using REST API

Hi, feeling a bit desperate here…

I’m trying to create a file upload step in Blueprint wizard and I’ve spent 4+ days trying to get it to work using jQuery/AJAX and the upload attachment API.

Does anyone have a working example of a file upload using jQuery? Alternately, is there another method I might not be aware of?

If you want to see my code, here’s what I have at the moment:

    wizard.on('submit.page1Id', function(e, state) {
		var files;
		$('input[type=file]').on('change', prepareUpload);

		function prepareUpload(event)
		{
		  files = event.target.files;
		  console.log(files);
		}		
		$('input[type=file]').trigger('change');

		console.log(files);

		var data = new FormData();
		data.append('file', files);
		
		console.log(data);
		console.log(files);
		console.log(files[0]);

		$.ajax
		  ({
			type: "POST",
			url: "http://localhost:1990/confluence/rest/api/content/917506/child/attachment",
			contentType:"multipart/form-data; boundary=------------------------021ab64c72420e84",
			async: false,
			cache: false,
			headers: {
				"Authorization": "Basic " + btoa("admin:admin"), "X-Atlassian-Token": "no-check"
			}, 
			data: JSON.stringify(data),
			success: function (){
				console.log('Attachment saved!'); 
			},
			error : function(xhr, errorText){
				console.log('Error '+ xhr.responseText);
			}
		});
    });

I’ve tried (and Googled) everything I can think of. It seems so simple yet I can’t seem to get anything to work.

The error I’m seeing at the moment is: “AbortError: Failed to execute ‘send’ on ‘XMLHttpRequest’: Failed to load ‘http://localhost:1990/confluence/rest/api/content/917506/child/attachment’.”

To give you a sense of the other things I’ve tried, so far I’ve:

  • Added/removed: “async: false,”
  • Added/removed: “cache: false,”
  • Added/removed: “processData: false,”
  • Changed contentType between “multipart/form-data”, “application/json”, and “false”
  • Added/removed: “JSON.stringify()” from the data

Everything seems to throw an error of some sort or doesn’t throw an error but aborts. Any suggestions would be much appreciated!

For anyone who needs it, this was solved thanks to Panos Karampis here: https://community.atlassian.com/t5/Confluence-questions/Upload-attachment-using-REST-API/qaq-p/458998/comment-id/77680#M77680