【问题标题】:Jqgrid upload file examplejqgrid上传文件示例
【发布时间】:2013-07-27 04:57:21
【问题描述】:

我有一个 jqgrid,我想上传一个文件,我得到了以下代码

 colModel: [
    { name: "id", index:"id", key: true,width: 30,editable: false },

    { name: "name", index:"name", width: 100,editable: true },           
    { name: "fileToUpload", 
      editoptions: {
          enctype: "multipart/form-data"
        }, 
        edittype:'file',
        index: 'fileToUpload', 
        width: 150,
        align: "left",
        editable: true },
], 

我使用jqgrid - upload a file in add/edit dialog中的函数

我的问题是我不知道如何使用 ajaxfileupload 函数的 url 文件将这个文件保存在数据库中,谁能给我一个例子?

谢谢!

【问题讨论】:

    标签: php jqgrid


    【解决方案1】:

    这是我的工作示例:

    colModel: 
    [
        {
            name: 'photo1',
            label: 'Фото1',
            index: 'photo1',
            search: false,
            editable: true,
            edittype: 'file',
            editoptions: {
                enctype: "multipart/form-data"
            },
            align: 'center',
        },
    ],
    

    然后 - 我使用了回调:

    afterSubmitCell: function (serverResponse, rowId, cellName, value, iRow, iCol) {
        var fileCell = (+rowId + 1) + '_' + cellName;
        var grid = $("#reports");
    
        var fileContainer = document.getElementById(fileCell).files;
        if (fileContainer !== null) {
            if (fileContainer.length > 0) {
                var file = fileContainer[0];
                var form = new FormData();
                form.append(cellName, file);
                form.append('id', rowId);
    
                $.ajax({
                    url: "/api/reports/save-file",
                    type: 'POST',
                    data: form,
                    processData: false,
                    contentType: false,
                    success: function (data) {
                        grid.jqGrid('setGridParam', {datatype: 'json'})
                                .trigger('reloadGrid', [{page: 1}]);
                    }
                });
            }
        }
    
        return [true, serverResponse]
    },
    

    然后将文件保存在我的后端部分(Laravel):

    if ($request->hasFile('photo1')) {
        $file = $request->file('photo1');
        if ($file->isValid()) {
            $fileName = rand(1000, 9999) . "." . $file->getClientOriginalExtension();
    
            //Put file on the server
            if (!empty($report)) {
                $report->attachFile($file, $fileName);
                $report->photo1 = $fileName;
                $response['status'] = $report->save();
            }
        }
    }
    

    【讨论】:

      猜你喜欢
      • 1970-01-01
      • 1970-01-01
      • 1970-01-01
      • 2012-07-02
      • 1970-01-01
      • 2021-10-20
      • 1970-01-01
      • 2019-07-14
      • 2017-03-08
      相关资源
      最近更新 更多