asp.net中Fine Uploader文件上传组件使用介绍(3)


  $('#triggerUpload').click(function() {
          uploader2.uploadStoredFiles();
      });


如果我们此时点击上传会发现.则提示上传失败. 因为还没有对上传服务器端做任何处理:

复制代码 代码如下:


    request:
      {
          endpoint: 'server/handlerfunction'
      },


这时我们需要在EndPoint指定处理文件上传的Php文件[这里是phpdemo].关于服务器端如果你没有已经成熟处理模块.还是推荐你使用官方Server目录上.这里我采用php环境则选中时php.php文件.对应客户端修改如下:

复制代码 代码如下:


   request:
   {
       endpoint: 'controller/php.php'
   }


打开php.php发现在文件头部说明该文件使用同时在文件定义三个类用来分别处理XMLHttpRequest、FormPost、BasicPost方式文件服务器端处理.在文件顶部注释中:

复制代码 代码如下:


  /****************************************
  Example of how to use this uploader class...
  You can uncomment the following lines (minus the require) to use
  hese as your defaults.

  // list of valid extensions, ex. array("jpeg", "xml", "bmp")
  $allowedExtensions = array();
  // max file size in bytes
  $sizeLimit = 10 * 1024 * 1024;
  //the input name set in the javascript
  $inputName = 'qqfile'

  require('valums-file-uploader/server/php.php');
  $uploader = new qqFileUploader($allowedExtensions, $sizeLimit, $inputName);

  // Call handleUpload() with the name of the folder, relative to PHP's getcwd()
  $result = $uploader->handleUpload('uploads/');

  // to pass data through iframe you will need to encode all html tags
  header("Content-Type: text/plain");
  echo htmlspecialchars(json_encode($result), ENT_NOQUOTES);

  /******************************************/


已经详细说明如下Class调用方式.添加如下Php代码即可简单完成服务器端处理:

复制代码 代码如下:


  $allowedExtensions = array("jpeg", "jpg", "bmp", "png");
  $sizeLimit = 10 * 1024 * 1024;
  $uploader = new qqFileUploader($allowedExtensions, $sizeLimit);
  $result = $uploader->handleUpload('uploads/'); //folder for uploaded files
  echo htmlspecialchars(json_encode($result), ENT_NOQUOTES);


allowExtensions则定义了允许上传文件的格式.

sizeLimit上限定义为10M.注意首先采用Phpinfo();方法输出当前php环境配置.一般默认情况默认上传文件最大大小为2M.如果你需要上传更大则修改php.ini文件配置参数 这里不再赘述.

uploder则是初始化qq.Fileuploder对象.并加载配置.

fineuploder调用处理上传函数.并传递服务器端存储上传文件存储路径.

echo想服务器端输出上传结果.必须.不然客户端接受不到指定responseJason参数用来判断上传后状态.

在进一步看看服务器端如何处理上传的找到handleUpload函数定义.

复制代码 代码如下:


     /**
       * Handle the uploaded file
       * @param string $uploadDirectory
       * @param string $replaceOldFile=true
       * @returns array('success'=>true) or array('error'=>'error message')
       */
      function handleUpload($uploadDirectory, $replaceOldFile = FALSE){
          if (!is_writable($uploadDirectory)){
              return array('error' => "Server error. Upload directory isn't writable.");
          }

          if (!$this->file){
              return array('error' => 'No files were uploaded.');
          }

          $size = $this->file->getSize();

          if ($size == 0) {
              return array('error' => 'File is empty');
          }

          if ($size > $this->sizeLimit) {
              return array('error' => 'File is too large');
          }

          $pathinfo = pathinfo($this->file->getName());
          $filename = $pathinfo['filename'];
          //$filename = md5(uniqid());
          $ext = @$pathinfo['extension'];        // hide notices if extension is empty

          if($this->allowedExtensions && !in_array(strtolower($ext), $this->allowedExtensions)){
              $these = implode(', ', $this->allowedExtensions);
              return array('error' => 'File has an invalid extension, it should be one of '. $these . '.');
          }

          $ext = ($ext == '') ? $ext : '.' . $ext;

          if(!$replaceOldFile){
              /// don't overwrite previous files that were uploaded
              while (file_exists($uploadDirectory . DIRECTORY_SEPARATOR . $filename . $ext)) {
                  $filename .= rand(10, 99);
              }
          }

          $this->uploadName = $filename . $ext;

          if ($this->file->save($uploadDirectory . DIRECTORY_SEPARATOR . $filename . $ext)){
              return array('success'=>true);
          } else {
              return array('error'=> 'Could not save uploaded file.' .
                  'The upload was cancelled, or server error encountered');
          }

      }   


在调用这个处理函数时.需要注意的是.传递的URL存储路径需要时绝对的.所以需要对传入路劲做一下格式化处理:

复制代码 代码如下:

内容版权声明:除非注明,否则皆为本站原创文章。

转载注明出处:https://www.heiqu.com/wjdjgg.html