无法使用C#将文件从jqgrid上传到数据库

时间:2015-01-12 14:42:22

标签: jqgrid

我正在尝试从JQgrid edittype文件选项上传文本文件。但是,当我单击提交按钮时,on add dialoge未调用Web方法。请帮忙。

以下是我的代码:

GRID:

jQuery("#JQGridFileUpload").jqGrid({

    postData: { searchString: '', searchField: '', searchOper: '' },

    datatype: function (postdata) {

        grid = $("#JQGridFileUpload")[0]; //its an array which contain binding data

        grid.addJSONData(objClientDocumentData);

    },

    jsonReader: {

        root: "rows",

        page: "page",

        total: "totalpages",

        records: "totalrecords",

        cell: "cell",

        id: "ClientDocumentid", //index of the column with the PK in it                  

        userdata: "userdata",

        repeatitems: true

    },

    height: 'auto',

    width: 'auto',

    colNames: ['ClientID', 'Document Type', 'Document Description', 'View/Upload documents'],

    colModel: [

        {

            name: 'ClientID', index: 'ClientID', width: 30, hidden: true,

            edittype: 'text', editable: true

        },

        {

            name: 'DocumentType', index: 'DocumentType', width: 30, editable: true,

            edittype: 'select', editoptions: { value: ClientDocumentType }

        },
        {

             name: 'DocumentName', index: 'DocumentName', width: 30, editable: true,

             edittype: 'file', editoptions: {

                 enctype: "multipart/form-data"

             },                           

             formatter: 'showlink',

             formatoptions: { baseLinkUrl: 'javascript:', showAction: "ShowFile('", addParam: "');" }

         }

    ],

    rowNum: 10,

    loadonce: true,

    pager: '#JQGridFileUploadPager',

    sortname: 'DocumentType',

    hidegrid: false,

    sortorder: 'desc',

    editurl: 'ClientMaster.aspx/UpdateDocumentDetails',

    viewrecords: true,

    caption: "Upload File",

    gridComplete: function () {
        ...
    }
});


$('#JQGridFileUpload').jqGrid('navGrid', '#JQGridFileUploadPager', ...)
{

    edit: true,

    add: true,

    del: true,

    search: false,

    refresh: true

},                                          

    //Edit functionality

    {  
        ...
    },

     //Add functionality

     {

        url: 'ClientMaster.aspx/InsertDocumentDetails',

        closeAfterAdd: true,

        reloadAfterSubmit: false,

        closeAfterAdd: true,

        afterSubmit: function (options, postData, response) {

        var filename = $("#DocumentName").val();

            var files = $("#DocumentName").val();

            $.fn.ajaxFileUpload

                (

                    {

                        url: 'ClientMaster.aspx/InsertDocumentDetails1',

                         secureuri: false,

                         fileElementId: 'DocumentName',

                         dataType: 'json',                                                                

                         async: false,

                         success: function (data, status) {



                             if (typeof (data.error) != 'undefined') {

                                 if (data.error != '') {

                                     alert(data.error);

                                 } else {

                                     alert(data.msg);

                                 }

                             }

                         },

                         error: function (data, status, e) {

                             debugger;

                             alert(e);

                         }

                     }

                 );

             return [true, '', false];



         }

     },

      //Delete functionality

     {



     }

 });

}


//////////////////////////////////////////////////////

This is my web method which is not getting called:

[WebMethod]

public static void InsertDocumentDetails(

HttpPostedFileBase  files

)

{
    ...
}

如何调用Web方法将文件上传到数据库?

1 个答案:

答案 0 :(得分:0)

我认为这是在实施中。 你在afterSubmit中调用ajaxFileUpload,这不是一个好的解决方案。 有一个PHP示例here

我会尝试为此准备一个在线演示。