在MVC使用Jquery上传文件 [英] Upload file using Jquery in MVC

查看:301
本文介绍了在MVC使用Jquery上传文件的处理方法,对大家解决问题具有一定的参考价值,需要的朋友们下面随着小编来一起学习吧!

问题描述

我需要使用jQuery从我的模态弹出来上传文件。
但我得到的 HttpPostedFileBase 为空Ajax调用期间。
谁能帮我出这一点。下面是jQuery的code:

 函数uploadDocument(的ContactID,TRIPID,文件){
如果($('#documentUploadInput')。VAL()==''){
    alertify.error('请选择要上传的文件');
    返回;
}
$阿贾克斯({
    网址:'/ CRMDomain / ContactDetail / UploadDocument',
    数据:{的ContactID:的ContactID,TRIPID:TRIPID,文件:文件},
    输入:POST,
    成功:函数(){
        alertify.success('上传');
        $('#CRMUploadDocumentModal')模态(隐藏)。
    },
    错误:函数(){
        alertify.error(不上传');
    }
});

}

这就是我所说的jQuery函数的方式:

 <输入ID =btnSave
       类型=按钮
       值=上传
       类=BTN BTN-默认的onclick ='uploadDocument($(#的ContactID)。VAL(),$(#TRIPID)。VAL(),$(#documentUploadInput)[0] .files [ 0]);' />


解决方案

您不能发布文件,这样容易与阿贾克斯。

这是我总是用最好的解决方案是使用的jQuery表单插件那么你应该改变你阿贾克斯 .ajaxForm

您可以检查这个答案了解更多详情。

I need to upload file using jquery from my modal popup. But I get the HttpPostedFileBase as null during ajax call. Can anyone help me out on this. Below is the jquery code:

function uploadDocument(contactId, tripId, file) {
if ($('#documentUploadInput').val() == '') {
    alertify.error('Please select a file to upload');
    return;
}
$.ajax({
    url: '/CRMDomain/ContactDetail/UploadDocument',
    data: { contactId: contactId, tripId: tripId, file: file },
    type: 'POST',
    success: function () {
        alertify.success('Uploaded');
        $('#CRMUploadDocumentModal').modal('hide');
    },
    error: function () {
        alertify.error('Not Uploaded');
    }
});

}

And this is the way i call the jquery function:

<input id="btnSave" 
       type="button" 
       value="Upload" 
       class="btn btn-default"  onclick='uploadDocument($("#ContactId").val(), $("#tripId").val(), $("#documentUploadInput")[0].files[0]);' />

解决方案

You can't post file so easy with Ajax.

The best solution that I always use is to use jQuery Form Plugin then you should just change your .ajax to .ajaxForm.

You can check this answer for more details.

这篇关于在MVC使用Jquery上传文件的文章就介绍到这了,希望我们推荐的答案对大家有所帮助,也希望大家多多支持IT屋!

查看全文
登录 关闭
扫码关注1秒登录
发送“验证码”获取 | 15天全站免登陆