0

I'm using Postman Rest API calls to upload file to Azure Blob. I want to understand how to attach files to the body of form data from frontend using C# code, so that it return me the result with some ID and File Type.

  • You've not said if you're using MVC C#, but the pattern would be something like [this](https://stackoverflow.com/a/37762290/519413) – Rory McCrossan Nov 24 '21 at 11:25
  • thank you! I'm trying to upload image file from front end using MVC C#, i wanted to know how to upload file to the body of form data. Would appreciate if anyone could support with this. – questionbank Nov 24 '21 at 11:56
  • In that case, the answer I linked to has the exact method you need to use – Rory McCrossan Nov 24 '21 at 14:05
  • Does this answer your question? [jQuery ajax upload file in asp.net mvc](https://stackoverflow.com/questions/2428296/jquery-ajax-upload-file-in-asp-net-mvc) – phentnil Nov 28 '21 at 17:32

1 Answers1

0

I think that this is a duplicated question

Adding the answer from the original post

document.getElementById('uploader').onsubmit = function () {
    var formdata = new FormData(); //FormData object
    var fileInput = document.getElementById('fileInput');
    //Iterating through each files selected in fileInput
    for (i = 0; i < fileInput.files.length; i++) {
        //Appending each file to FormData object
        formdata.append(fileInput.files[i].name, fileInput.files[i]);
    }
    //Creating an XMLHttpRequest and sending
    var xhr = new XMLHttpRequest();
    xhr.open('POST', '/Home/Upload');
    xhr.send(formdata);
    xhr.onreadystatechange = function () {
        if (xhr.readyState == 4 && xhr.status == 200) {
            alert(xhr.responseText);
        }
    }
    return false;
}   
Karnafun
  • 96
  • 6