【问题标题】:JQGrid File UploadJQGrid 文件上传
【发布时间】:2010-08-26 17:33:54
【问题描述】:

我想通过 JQGrid 内置的内联/弹出式编辑使用多文件上传。用户将提供一些元信息和文件。元信息将进入数据库,文件将保存在网络服务器上。我需要在提交按钮上执行此操作。有什么建议/参考吗?

【问题讨论】:

    标签: file upload jqgrid


    【解决方案1】:

    这个问题有点老了,但如果有人在谷歌上得到它,这里:

    http://www.trirand.com/blog/?page_id=393/feature-request/file-upload-again/

    【讨论】:

      【解决方案2】:

      这是 JqGrid 工作人员基于Ajax file upload 插件编写的demo。 这是一个非常简单的脚本!

      你有你的grid.php

      // add column "fileToUpload" , hidden in main table view, showed in add/edit forms
      $grid->addCol(array("name"=>"fileToUpload", "hidden"=>true, "editable"=>true, "edittype"=>"file", "editrules"=>array("edithidden"=>true)));
      
      // file upload code
      $upload = <<< UPLOAD
      
      function (formid) {
      
          //These are needed for fileupload plugin
          $(formid).attr("method", "POST");
          $(formid).attr("action", "");
          $(formid).attr("enctype", "multipart/form-data");
      
          // convert to jqueryUI button
      
          $("#fileToUpload", formid).button();
      
          // Create a button bellow the file field
          $("<BR/><button id='buttonUpload'>Upload</button>").button().insertAfter("#fileToUpload", formid);
      
          // bind a event
      
          $("#buttonUpload", formid).click(function () {
      
              $('<img src="loading.gif" />')
                  .dialog()
                  .ajaxStart(function () {
                      $("#gview_grid").attr("disabled", true);  //disable jqgrid to avoid editing while uploading file
                      $(this).show();
                  })
                  .ajaxComplete(function () {
                      $(this).dialog("close");
                      $("#gview_grid").removeAttr("disabled");;  //restore jqgrid after file loading complete
                  });
      
      
              $.ajaxFileUpload({
                  url: 'doajaxfileupload.php',
                  secureuri: false,
                  fileElementId: 'fileToUpload',
                  dataType: 'json',
      
      
                  success: function (data, status) {
      
      
                      console.log(data);
                      if (typeof (data.error) != 'undefined') {
                          if (data.error != '') {
                              /* if file-upload error */
                              alert(data.error);
                          } else {
                          // file successfully uploaded 
                              alert(data.msg);
                          }
                      }
                  },
                  error: function (data, status, e) {
                      alert(e);
                  }
              });
              return false;
          });
      }
      UPLOAD;
      
      // bind $upload code to onInitializeForm
      $grid->setNavEvent('add', 'onInitializeForm', $upload);
      

      和 Ajax 文件上传器 (doajaxfileupload.php):

      <?php
          $error = "";
          $msg = "";
          $fileElementName = 'fileToUpload';
          if(!empty($_FILES[$fileElementName]['error']))
          {
              switch($_FILES[$fileElementName]['error'])
              {
      
                  case '1':
                      $error = 'The uploaded file exceeds the upload_max_filesize directive in php.ini';
                      break;
                  case '2':
                      $error = 'The uploaded file exceeds the MAX_FILE_SIZE directive that was specified in the HTML form';
                      break;
                  case '3':
                      $error = 'The uploaded file was only partially uploaded';
                      break;
                  case '4':
                      $error = 'No file was uploaded.';
                      break;
      
                  case '6':
                      $error = 'Missing a temporary folder';
                      break;
                  case '7':
                      $error = 'Failed to write file to disk';
                      break;
                  case '8':
                      $error = 'File upload stopped by extension';
                      break;
                  case '999':
                  default:
                      $error = 'No error code avaiable';
              }
          }elseif(empty($_FILES[$fileElementName]['tmp_name']) || $_FILES[$fileElementName]['tmp_name'] == 'none')
          {
              $error = 'No file was uploaded..';
          }else 
          {
                  $msg .= " File Name: " . $_FILES[$fileElementName]['name'] . ", ";
                  $msg .= " File Size: " . @filesize($_FILES[$fileElementName]['tmp_name']);
                  //for security reason, we force to remove all uploaded file
                  //@unlink($_FILES[$fileElementName]);
                  $fname = basename($_FILES[$fileElementName]['name']);
                  if (move_uploaded_file($_FILES[$fileElementName]['tmp_name'], $fname)) {
                      $msg .= " OK";
                  } else {
                      $error= 'Possible file upload attack';
                      @unlink($_FILES['fileToUpload']);
                  }           
                  //@unlink($_FILES['fileToUpload']);     
      
          }       
          echo "{";
          echo                "error: '" . $error . "',\n";
          echo                "msg: '" . $msg . "'\n";
          echo "}";
      ?>
      

      【讨论】:

        猜你喜欢
        • 2013-07-27
        • 2012-07-02
        • 1970-01-01
        • 1970-01-01
        • 2018-04-02
        • 2013-08-06
        • 1970-01-01
        • 2011-11-24
        • 1970-01-01
        相关资源
        最近更新 更多