BOX-API上传文件表格 [英] BOX-API upload file form

查看:176
本文介绍了BOX-API上传文件表格的处理方法,对大家解决问题具有一定的参考价值,需要的朋友们下面随着小编来一起学习吧!

问题描述

我正在尝试使用php和Zend框架在BOX_API中上传文件。但我想念的东西。这是我第一次使用这样的界面,或者我已经阅读手册。但这对我来说是困惑的。我的问题是两个:

i'm trying to upload a file in BOX_API with php and Zend framework. But i'm missing somethin. It's the first time that i use an interface like this o i've read the manual. But it is confusig for me. My question are two:

-首先,为什么您必须仅将文件名而不是带有正确文件头的整个文件传递给post调用上传?表单中的文件上传不同于通过邮寄传递文件名;

-First, why do you have to pass to the post call only the name of the file and not the whole file with the right header for file upload? File upload in a form is not like passing the name of the file through a post call;

-第二,因此,我是否必须为文件上传创建表单?

-secondly and consequently, do i have to make a form for file upload or simply a textarea where to write the name of the file to be passed to the BOX-API?

更新:
这是我上载表格的代码:

UPDATE: This is the code of my upload form:

    $form = new Zend_Form;
    $form->setAction('/imball-reagens/public/upload')
    ->setMethod('post');
    $file = new Zend_Form_Element_File('file');
    $file->setLabel('Choose a file to upload:');
    $file->addValidator('alnum');
    $file->setRequired(true);
    $form->addElement($file);
    $access_token = new Zend_Form_Element_Hidden(array('name' => 'access_token', 'value' => $result->access_token));
    $form->addElement($access_token);
    $refresh_token = new Zend_Form_Element_Hidden(array('name' => 'refresh_token', 'value' => $result->refresh_token));
    $form->addElement($refresh_token);
    $form->addElement('submit', 'upload', array('label' => 'Upload File'));
    echo $form;

这是Box API的POST cal,形式为:

And this s the POST cal to the box API which comes after the form:

    $access_token= $this->getRequest()->getParam('access_token');
    $client = new Zend_Http_Client('https://upload.box.com/api/2.0/files/content');
    $client->setMethod(Zend_Http_Client::POST);
    $client->setHeaders('Authorization: Bearer '.$access_token);
    $data = $_FILES["file"]["name"];
    $client->setParameterPost(array(
            'filename'  => '@'.$data,
            'parent_id' => '0'
    ));
    $response = $client->request()->getBody();
    $this->view->response= $response;
    $result = json_decode($response);

它引发的错误如下:

 {"type":"error","status":400,"code":"invalid_request_parameters","help_url":"http:\/\/developers.box.com\/docs\/#errors","message":"Invalid input parameters in request","request_id":"172518183652dcf2a16af73"}


推荐答案

棘手的调试过程中没有看到所有代码,但在您粘贴的位看来,您正在传递 $ _FILES [ file] [ name] 到API-它仅包含用户上传的文件的原始名称-您需要将该位置传递给服务器上的文件将数据发送到Box API客户端,以便它可以抓取数据并将其发送到Box服务器-该数据应存储在 $ _ FILES [ file] [ tmp_name]

Tricky to debug without seeing all of the code, but in the bit you pasted it looks like you are passing $_FILES["file"]["name"] to the API - this only contains the original name of the file which was uploaded by the user - you need to pass the location to the file on the server which is sending the data to the Box API client so that it can grab it and send it to the Box server - this should be stored in $_FILES["file"]["tmp_name"].

我建议将代码更改为此,然后重试:

I would recommend changing the code to this and trying again:

$access_token= $this->getRequest()->getParam('access_token');
$client = new Zend_Http_Client('https://upload.box.com/api/2.0/files/content');
$client->setMethod(Zend_Http_Client::POST);
$client->setHeaders('Authorization: Bearer '.$access_token);
$data = $_FILES["file"]["tmp_name"];
$client->setParameterPost(array(
    'parent_id' => '0'
));
$client->setFileUpload($data, 'filename');
$response = $client->request()->getBody();
$this->view->response= $response;
$result = json_decode($response);

这篇关于BOX-API上传文件表格的文章就介绍到这了,希望我们推荐的答案对大家有所帮助,也希望大家多多支持IT屋!

查看全文
登录 关闭
扫码关注1秒登录
发送“验证码”获取 | 15天全站免登陆