Not receiving any data from file upload

795 views Asked by At

I am trying to use the Krajee Bootstrap File Input to upload a file. I am using ASP.NET Core 2. Here is my HTML:

<input id="fileFileUpload" type="file" />

Here is my javascript code:

    $("#fileFileUpload").fileinput({
        showPreview: true,
        uploadAsync: false,
        uploadUrl: '/fileupload'
    });

Here is my controller code:

[HttpPost]
public JsonResult Index(List<IFormFile> files)
{
    // Do stuff
}

I am putting a breakpoint on my controller method and the breakpoint is being hit. However files is empty. How can I retrieve the file that was uploaded?

2

There are 2 answers

1
Matan Shahar On BEST ANSWER

The number one reason I can see for that code not work is that your input in not assigned a name.

<input id="fileFileUpload" name="files" type="file" />

Other then that you can follow this MSDN article.

0
The Alster On

You can access the files from another button and then post them using ajax as normal. ButtonFileUpload8 is the ID for the new button, file-8 is the ID for the file upload control. You have to disable the upload button

This is not perfect code, I just put it together to show how its done.

HTML:

    <form enctype="multipart/form-data">
        <div class="row mb-2">
            <div class="col-md-8">
                <div class="file-loading">
                    <input id="file-8" type="file" multiple>
                </div>
            </div>
        </div>
        <div class="btn-group btn-group-sm mr-1" role="group">
            <button id="ButtonFileUpload8" type="button" class="btn btn-sm">
                Blue
            </button>
        </div>

    </form>

Javascript (note this ignores the post URL set in the initiator so calls FileUploadKrajee instead and uses ajax):

    $("#file-8").fileinput({
        uploadUrl:"@Url.Action("FileUploadKrajee", "App")",
        uploadAsync: true,
        minFileCount: 1,
        maxFileCount: 5,
        overwriteInitial: false,
        initialPreview: "",
        initialPreviewConfig: "",
        uploadExtraData: "",
        showUpload: false
    });

    //  Click of upload button

    $("#ButtonFileUpload8").click(function () {
        var control = document.getElementById("file-8");
        var files = control.files;
        var formData = new FormData();

        for (var i = 0; i != files.length; i++) {
            formData.append("files", files[i]);
        }

        $.ajax({
            url: 'UploadFiles3',
            type: "POST",
            contentType: false, // Do not set any content header
            processData: false, // Do not process data
            data: formData,
            async: false,
            success: function (result) {
                if (result != "") {
                    alert(result);
                }
                control.files.value("");
            },
            error: function (err) {
                alert(err.statusText);
            }
        });
    });

Then in the receiving controller you can use:

    public ActionResult UploadFiles3(List<IFormFile> files)
    {
        string nams = "";
        string funame = "";

        foreach (IFormFile source in files)
        {
            string filename = ContentDispositionHeaderValue.Parse(source.ContentDisposition).FileName.ToString();

            filename = this.EnsureCorrectFilename(filename);

            nams = nams + source.FileName.ToString();

            funame = "D:\\Data\\PointsAlign\\Core\\" + filename;

            FileStream output = System.IO.File.Create(funame);

            source.CopyTo(output);
        }

        return Json("Hi, Alster. Your files uploaded successfully " + nams);

    }

This does work but I haven't stress tested it yet for any issues