Javascript jqgrid - 在添加/编辑对话框中上传文件

声明:本页面是StackOverFlow热门问题的中英对照翻译,遵循CC BY-SA 4.0协议,如果您需要使用它,必须同样遵循CC BY-SA许可,注明原文地址和作者信息,同时你必须将它归于原作者(不是我):StackOverFlow 原文地址: http://stackoverflow.com/questions/7550304/
Warning: these are provided under cc-by-sa 4.0 license. You are free to use/share it, But you must attribute it to the original authors (not me): StackOverFlow

提示:将鼠标放在中文语句上可以显示对应的英文。显示中英文
时间:2020-08-24 02:41:09  来源:igfitidea点击:

jqgrid - upload a file in add/edit dialog

javascriptjqueryjqgrid

提问by Dranix

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?

This is my code:

我是 jqgrid 的新手,通过您的回答我学到了很多东西。
现在我有一个问题:我想在向jqgrid添加或修改记录时上传文件?

这是我的代码:

{
    name: 'File',
    index: 'file',
    hidden: true,
    enctype: "multipart/form-data",
    editable: true,
    edittype: 'file',
    editrules: {
        edithidden: true,
        required: true
    },
    formoptions: {
        elmsuffix: '*'
    }
}



However the field I got in controller always be null :(. Any suggestion
Anyone know working example?
Thanks in advance

UPDATE
I have found a very good example at http://tpeczek.codeplex.com/releases



然而,我在控制器中得到的字段始终为空 :(。任何建议
有人知道工作示例吗?
提前致谢

更新
我在http://tpeczek.codeplex.com/releases找到了一个很好的示例

采纳答案by hetu

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

我昨天才开始工作..这是我用于文件上传的 colModel 列,

{
    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.

您必须设置afterSubmit: UploadImage。它仅在数据已发布且响应已返回后才上传文件。我在这里检查,如果插入成功,则只开始上传否则显示错误。我使用过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!');
            }
        }
    )          }