jqgrid - upload a file in add/edit dialog

后端 未结 1 347
盖世英雄少女心
盖世英雄少女心 2020-12-03 06:12

I\'m new to jqgrid and I have learn many things through your answer.
Now I have a problem: I want to upload files when adding or modifying records to a jqgrid?

相关标签:
1条回答
  • 2020-12-03 07:06

    I got it working just yesterday..here's my colModel column for file upload,

    {
        name: 'fileToUpload',
        index: 'customer_id',
        align: 'left',
        editable: true,
        edittype: 'file',
        editoptions: {
            enctype: "multipart/form-data"
        },
        width: 210,
        align: 'center',
        formatter: jgImageFormatter,
        search: false
    }
    

    You have to set afterSubmit: UploadImage. It uploads the file only after data has been post & response has come back. I'm checking here that if insert was succesfful then only start upload else show error. I've used Jquery Ajax File Uploader.

    function UploadImage(response, postdata) {
    
        var data = $.parseJSON(response.responseText);
    
        if (data.success == true) {
            if ($("#fileToUpload").val() != "") {
                ajaxFileUpload(data.id);
            }
        }  
    
        return [data.success, data.message, data.id];
    
    }
    
    function ajaxFileUpload(id) 
    {
        $("#loading")
        .ajaxStart(function () {
            $(this).show();
        })
        .ajaxComplete(function () {
            $(this).hide();
        });
    
        $.ajaxFileUpload
        (
            {
                url: '@Url.Action("UploadImage")',
                secureuri: false,
                fileElementId: 'fileToUpload',
                dataType: 'json',
                data: { id: id },
                success: function (data, status) {
    
                    if (typeof (data.success) != 'undefined') {
                        if (data.success == true) {
                            return;
                        } else {
                            alert(data.message);
                        }
                    }
                    else {
                        return alert('Failed to upload logo!');
                    }
                },
                error: function (data, status, e) {
                    return alert('Failed to upload logo!');
                }
            }
        )          }                                                                                            
    
    0 讨论(0)
提交回复
热议问题