douxian6260 2013-07-26 19:32
浏览 121

Jqgrid上传文件示例

I have a jqgrid and I'd like to upload a file, I got the following code

 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 },
], 

and I use the function like in jqgrid - upload a file in add/edit dialog

My problem is that I don't know how to use the url file of the ajaxfileupload function to save this file in a database , Can anyone show me an example??

Thanks!

  • 写回答

1条回答 默认 最新

  • dsj60862 2016-08-11 15:00
    关注

    This is my working example:

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

    Then - I have used callback:

    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]
    },
    

    And then save file on my backend-part (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();
            }
        }
    }
    
    评论

报告相同问题?

悬赏问题

  • ¥15 装 pytorch 的时候出了好多问题,遇到这种情况怎么处理?
  • ¥20 IOS游览器某宝手机网页版自动立即购买JavaScript脚本
  • ¥15 手机接入宽带网线,如何释放宽带全部速度
  • ¥30 关于#r语言#的问题:如何对R语言中mfgarch包中构建的garch-midas模型进行样本内长期波动率预测和样本外长期波动率预测
  • ¥15 ETLCloud 处理json多层级问题
  • ¥15 matlab中使用gurobi时报错
  • ¥15 这个主板怎么能扩出一两个sata口
  • ¥15 不是,这到底错哪儿了😭
  • ¥15 2020长安杯与连接网探
  • ¥15 关于#matlab#的问题:在模糊控制器中选出线路信息,在simulink中根据线路信息生成速度时间目标曲线(初速度为20m/s,15秒后减为0的速度时间图像)我想问线路信息是什么