Solution 1 :

You could use form submit or use ajax to post the form and use IFormFile on action to receive your file.In the action, then you could get file name or convert file to byte[].

The name of action parameter is required to match the name of form data.

For example:


    $('#submitAddFile').click(function (e) {
        var file = $('#fileInput')[0].files;
        var formData = new FormData();
        formData.append("myFile", file[0]);

            type: "POST",
            url: "/Home/UploadFile",
            contentType: false,
            processData: false,
            data: formData,
            success: function (result) {
            error: function () {
                alert("there was an error");


public void UploadFile(IFormFile myFile)
        var fileName = Path.GetFileName(myFile.FileName);
        using (var ms = new MemoryStream())
            byte[] fileBytes = ms.ToArray();

            //save fileName and fileBytes into database


Problem :

I have a custom input field, in which I want to upload a file.

<form class="submitform">
    <div class="input-group">
            <input type="file" class="custom-file-input" id="fileInput"
                   aria-describedby="fileInput" />
            <label class="custom-file-label" for="fileInput">Choose file</label>
    <div class="modal-footer row">
        <button type="submit" class="btn btn-primary" id="submitAddFile">Submit</button>

Once, the submit button in my form is pressed, I want to fetch respectively the filename and the byte[].
This I try to do in the following way through jQuery:

$('#submitAddFile').click(function () {
    var file = $('#fileInput')[0].files;

And then I sort of didn’t get any further.. From what I can read through debug, I’m not getting any byte[] that I can pull out and send to my controller. I am however able to get things, such as file size and file name.

Therefore, how do i correctly extract the byte[] so that I can store it in my database


Comment posted by Rory McCrossan

You’re over complicating this. Just send the file to the server (either by submitting the form or AJAX) and your ASP.Net action will handle the file’s binary data for you, you just need to save it somewhere.

Comment posted by Jeppe Christensen

Right, might as well do that! What type of post is that?

Comment posted by Jeppe Christensen

Is it possible for you to provide a bit more explanation to this?

Comment posted by here’s


Comment posted by Jeppe Christensen

But does this work in .net Core? the

Comment posted by Jeppe Christensen

Thank you! can you please let me know, why you would use the MemoryStream? In which cases is this necessary? I can see it works without

Comment posted by…

@Jeppe Christensen Refer to