0

I'm writing application where I need to upload file ajax I used jQuery.form library but the action go to the controller with empty list of files I don't know why here is my code html:

<form id="well-log-form" method="post" enctype="multipart/form-data">

            <div class="fileUpload btn btn-primary">
                <span>Well Logs</span>
                <input type="file" id="well-logs" class="upload" />
            </div>
        </form>

and Js Code is :

   document.getElementById("well-logs").onchange = function () {

    var _url = "/Importer/WellLogUpload";
    var options = {         
        beforeSubmit: showRequest,  

        url: _url,
        type: 'post'        

    };
    $('#well-log-form').ajaxSubmit(options);
};
function showRequest(formData, jqForm, options) {
    return true;
}

function showResponse(responseText, statusText, xhr, $form) {
   // $("body").append(responseText);
}

could any one help, I think it should work but I don't know why it is not working.

1 Answer 1

1

try this in jquery, it will post your file.

//#file is the id of { <input type="file" id="file"> }

$("#file").change(function () {

    var file_data = $(this).prop("files")[0];
    var form_data = new FormData();
    form_data.append("file", file_data)

    $.ajax({
        url: "your url",
        type: "post",
        data: form_data,
        contentType: false,
        processData: false,
        success: function (path) {
           //on success
        }
    });

});
Sign up to request clarification or add additional context in comments.

Comments

Your Answer

By clicking “Post Your Answer”, you agree to our terms of service and acknowledge you have read our privacy policy.

Start asking to get answers

Find the answer to your question by asking.

Ask question

Explore related questions

See similar questions with these tags.