简体   繁体   English

上传文件jQuery ajax MVC

[英]Upload File jQuery ajax MVC

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: 我正在编写需要上传文件ajax的应用程序,我使用的是jQuery.form库,但是操作转到具有空文件列表的控制器,我不知道为什么这是我的代码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 : Js代码是:

   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. 谁能帮忙,我认为它应该起作用,但我不知道为什么它不起作用。

try this in jquery, it will post your file. 尝试在jQuery中,它将发布您的文件。

//#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
        }
    });

});

声明:本站的技术帖子网页,遵循CC BY-SA 4.0协议,如果您需要转载,请注明本站网址或者原文地址。任何问题请咨询:yoyou2525@163.com.

 
粤ICP备18138465号  © 2020-2024 STACKOOM.COM