【问题标题】:cant get image size and type using the jQuery File Upload plugin, how to?无法使用 jQuery File Upload 插件获取图像大小和类型,怎么办?
【发布时间】:2014-03-14 21:26:59
【问题描述】:

我正在使用jQuery-File-Upload jquery 插件上传一些文件 我正在通过 api 将图片从一个域(客户端)上传到另一个域(服务器端)。

图像似乎可以很好地上传到服务器端的 tmp 目录,但 $_FILE var 不包含图像类型或大小

array (size=1)
  'files' => 
    array (size=5)
      'name' => 
        array (size=1)
          0 => string '3.jpg' (length=5)
      'type' => 
        array (size=1)
          0 => null
      'tmp_name' => 
        array (size=1)
          0 => string '/tmp/phpXyHG5T' (length=14)
      'error' => 
        array (size=1)
          0 => int 0
      'size' => 
        array (size=1)
          0 => null

js很简单

$('#fileupload').fileupload({
        dataType: 'json',
        url : 'http://server_side.com/requests/index/image-upload/',
        add: function (e, data) {
            data.context = $('#upload_button')
                .click(function () {
                    data.context = $('#upload_button').text('Uploading...');
                    data.submit();
                });
        },
        done: function (e, data) {
            console.log(data);
        }
    }).on('fileuploaddone', function (e, data) {
        console.log(data);
    }).on('fileuploadsubmit', function (e, data) {
        console.log(data);
    });

php端

$this->_helper->layout->disableLayout();
$this->_helper->viewRenderer->setNoRender(TRUE);

// initialize the upload
$uploadHandler = new Api_Model_UploadHandler();
$uploadHandler->initialize();

有什么想法吗?

【问题讨论】:

    标签: php jquery zend-framework jquery-file-upload image-upload


    【解决方案1】:

    我想通了。 我正在使用 Zend 框架。

    问题是 $_FILE 丢失了 sizetype 如果您在 Api_Model_UploadHandler 初始化之后请求它,因为 tmp 图像被删除。

    所以之前询问图像似乎可以正常工作。

    无需检查文件是否存在

    public function imageUploadAction()
    {
        // in case some other crap goes wrong on the website, i want a clean json response
        error_reporting(0);
    
        $this->_helper->layout->disableLayout();
        $this->_helper->viewRenderer->setNoRender(TRUE);
    
        // get the request
        $request   = $this->getRequest();
        // get other params if you sent any
    
        $fileName = !empty($_FILES) ? $_FILES["files"]["name"][0] : '_';
    
        if (file_exists('tmp/' . $fileName)) {
    
            $upload = new Zend_File_Transfer_Adapter_Http();
    
            if (is_null($files)) {
                $files = $upload->getFileInfo();
            }
    
                    // ... do a regular zend image upload
        }
    
        // initialize the upload
        $uploadHandler = new Api_Model_UploadHandler();
        $uploadHandler->initialize();
    
        return json_encode(array());
    }
    

    【讨论】:

      猜你喜欢
      • 1970-01-01
      • 1970-01-01
      • 2016-03-13
      • 2012-05-11
      • 1970-01-01
      • 1970-01-01
      • 1970-01-01
      • 1970-01-01
      • 2013-08-01
      相关资源
      最近更新 更多