jQuery - FileUpload插件 - PHP UploadHandler

时间:2014-03-28 22:55:53

标签: php jquery wordpress file-upload

我设置了jQuery FileUpload(基本版本,如http://blueimp.github.io/jQuery-File-Upload/basic.html所示),并将我的文件上传到server/php/files目录就好了。

对于常规input type="file",我使用了以下代码:

    // if there's an CV already and no file was submitted
    if ($_POST['cv_id'] && !$_FILES['file']['tmp_name']) {
        // set current CV and job offer reply relationship
        $module_name = 'Documents';
        $link_field_name = 'hrjob_jobofferreplies_documents';
        $sugar->set_relationship($module_name, $_POST['cv_id'], $link_field_name, $job_offer_reply_id['id']);

    }

    // if a file was submitted and there's another CV already
    if ($_POST['cv_id'] && $_FILES['file']['tmp_name']) {
        // set status of old CV to inactive
        $values = array(
            'id' => $_POST['cv_id'],
            'status_id' => 'Expired'
        );
        $sugar->set('Documents', $values);

    }


// if CV was submitted
if ($_FILES['file']['tmp_name']) {
        $handle = fopen($_FILES['file']['tmp_name'], "rb");
        $filename = $_FILES['file']['name'];
        $contents = fread($handle, filesize($_FILES['file']['tmp_name']));
        $binary = base64_encode($contents);

        // create document in SugarCRM
        $values = array(
             'filename' => $filename,
             "document_name" => "Job Offer Reply",
             'category_id' => 'Resume',
             'status_id' => 'Active'
        );

        $doc_id = $sugar->set('Documents',$values);

        // create revision no. 1 to actually upload the document
        $sugar->set_document_revision($doc_id['id'],1,$binary,$filename);

    }

此代码位于自定义Wordpress插件中的函数内。

使用jQuery FileUpload插件的正确方法是什么?我应该以某种方式调用此代码,还是应该使用UploadHandler类?

1 个答案:

答案 0 :(得分:0)

你必须保留一切(如果不是SugarCRM和WP之间的桥梁)将被打破。 要使用Jquery上传,您需要删除上传调用:

$sugar->set_document_revision($doc_id['id'],1,$binary,$filename);

在Jquery上传中声明var cv_id。 检查方法set_document_revision