1

I am trying to upload a file to Azure Devops as an attachment via a static web-site. I can create new work-items without any issues, however can't seem to get the attachment upload to work.

var fileInput = document.getElementById('upload');
var file = fileInput.files[0];
var fileName = file.name;

//Binary file type
var r = new FileReader();
    r.readAsBinaryString(file);

$.ajax({
    url: attachUrl,
    type: 'POST',
    data: r,
    cache: false,
    processData: false,
    contentType: false,
    success: function(response) {
        alert("File uploaded");
    },
    beforeSend: function (xhr) {
        xhr.setRequestHeader("Authorization", "Basic " + btoa("" + ":" + '[AUTH]'));
    },
    error: function(XMLHttpRequest, textStatus, errorThrown) { 
        alert("ERROR");
    },
    complete: function () {
    }
    });
    return false;

fileInput, file & r all seem to populate as expected.

I am getting error 400 when submitting as above. Am I doing something glaringly stupid? I have been testing with a simple .txt file.

Any help greatly appreciated :)

Colin Dawson
  • 133
  • 3
  • 16

1 Answers1

0

Turns out I was being silly. Was missing the .result on the FileReader()

    //Binary file type
var r = new FileReader();
    r.readAsBinaryString(file);

$.ajax({
    url: attachUrl,
    type: 'POST',
    data: r.result,

Onwards!

Colin Dawson
  • 133
  • 3
  • 16