【问题标题】:File Upload with Restler v3使用 Restler v3 上传文件
【发布时间】:2013-03-31 19:31:45
【问题描述】:

就在最近,Restler v3 (source) 添加了对 multipart/form-data 上传 的支持,但我无法让它工作。在我的 index.php 文件中,我添加了:

$r->setSupportedFormats('JsonFormat', 'UploadFormat');

当我发布一个 .txt 文件时,我收到以下错误(这是意料之中的,因为默认的“允许”格式是“image/jpeg”、“image/png”:

"error": {
    "code": 403,
    "message": "Forbidden: File type (text/plain) is not supported."
}

但是当我发布一个 .jpg 文件时,我得到了以下错误:

"error": {
    "code": 404,
    "message": "Not Found"
}

我错过了什么?这是我的功能:

function upload() {
    if (empty($request_data)) {
        throw new RestException(412, "requestData is null");
    }
    return array('upload_status'=>'image uploaded successfully');
}

【问题讨论】:

    标签: php api rest restler


    【解决方案1】:

    我想通了!我只需要一个post() 函数!对于遇到与我遇到的相同问题的任何人,这是我使用 Restler 3 上传文件的解决方案:

    index.php

    <?php
        require_once '../vendor/restler.php';
        use Luracast\Restler\Restler;
    
        $r = new Restler();    
        $r->addAPIClass('Upload');  
        $r->setSupportedFormats('JsonFormat', 'UploadFormat');
    
        $r->handle();
    

    上传.php

    <?php
        class Upload {
            function get(){
               if (empty($request_data)) {
                  throw new RestException(412, "requestData is null");
               }
            }
    
            function post($request_data=NULL) {
               return array('upload_status'=>'image uploaded successfully!');
            }
        }
    

    【讨论】:

      猜你喜欢
      • 2013-08-31
      • 1970-01-01
      • 1970-01-01
      • 1970-01-01
      • 1970-01-01
      • 1970-01-01
      • 2013-02-27
      • 1970-01-01
      • 1970-01-01
      相关资源
      最近更新 更多