使用Ajax Post将Image和其他参数传递给控制器

时间:2019-02-02 12:17:46

标签: jquery json ajax

当我仅发送图片时

$.ajax({
       url: '@Url.Action("SaveMeterImageDetails", "saveimage")',
       type: "POST",
       processData: false,
       contentType: false,
       data: data, 
       success: function (response) {
        alert("uploaded");
       }
 });

但是我也需要发送一个ID,所以我不能这样做 请帮助

$.ajax({
                url: '@Url.Action("SaveMeterImageDetails", "saveimage")',
                type: "POST",
                processData: false,
                contentType: false,
                data: { ProfileID: ProfileID, helpSectionImages: data},
                success: function (response) {
                    alert("uploaded");
                }
             });

1 个答案:

答案 0 :(得分:1)

您必须使用表单数据来解析图像以及服务器上的其他参数(如给定示例)

    var form = $('#fileUploadForm')[0];

    // Create an FormData object 
    var data = new FormData(form);

    // If you want to add an extra field for the FormData
    data.append("ProfileID", ProfileID);


    $.ajax({
        type: "POST",
        enctype: 'multipart/form-data',
        url: URL,
        data: data,
        processData: false,
        contentType: false,
        cache: false,

        success: function (data) {


        },
        error: function (e) {

        }
    });