【问题标题】:Apigility file upload multipart/form-dataApigility 文件上传 multipart/form-data
【发布时间】:2017-10-02 10:20:04
【问题描述】:

我的文件上传也有问题。 提交以下表单后,我从 API 获得以下 JSON 响应。我正在使用 multipart/form-data

{validation_messages: {file_attachment: []},…}
  detail: "Failed Validation"
  status: 422
  title: "Unprocessable Entity"
  type: "http://www.w3.org/Protocols/rfc2616/rfc2616-sec10.html"
  validation_messages: {file_attachment: []}
  file_attachment: []


Apigility 验证器:

0 => array(
            'required' => true,
            'validators' => array(),
            'filters' => array(
                0 => array(
                    'name' => 'Zend\\Filter\\File\\RenameUpload',
                    'options' => array(
                        'target' => 'data/',
                        'randomize' => true,
                    ),
                ),
            ),
            'name' => 'file_attachment',
            'type' => 'Zend\\InputFilter\\FileInput',
            'allow_empty' => false,
            'continue_if_empty' => false,
        ),
        1 => array(
            'required' => false,
            'validators' => array(),
            'filters' => array(),
            'name' => 'title',
        ),


这是我的表格:

    <form id="avataruploadform" class="form-horizontal">
    <fieldset>

        <!-- Form Name -->
        <legend>Form Name</legend>

        <!-- Text input-->
        <div class="form-group">
            <label class="col-md-4 control-label" for="title">Title</label>
            <div class="col-md-4">
                <input id="title" name="title" type="text" placeholder="Some title for your sample file"
                       class="form-control input-md" required=""> <span
                    class="help-block">Sample field (title)</span>
            </div>
        </div>

        <!-- File Button -->
        <div class="form-group">
            <label class="col-md-4 control-label" for="file_attachment">     (PDF)</label>
            <div class="col-md-4">
                <input id="file_attachment" name="pdf" class="input-file" type="file">
            </div>
        </div>

        <!-- Button -->
        <div class="form-group">
            <label class="col-md-4 control-label" for="submit">Submit</label>
            <div class="col-md-4">
                <button id="submit" name="submit" class="btn btn-primary">Submit AJAX upload with file</button>
            </div>
        </div>
    </fieldset>
</form>


jQuery(AJAX 请求):

<script type="application/javascript">
    jQuery(document).ready(function() {
        jQuery('#avataruploadform').submit(function(e) {
            e.preventDefault();

            // Note: if you observe 422 responses, check what's assembled into fd amd
            // that it looks correct.
            var fd = new FormData();
            fd.append('file_attachment', $('#file_attachment')[0].files[0]);

            jQuery.ajax({
                url : 'http://localhost:8080/api/upload/avatar', // Specify the path to your API service
                type : 'POST',              // Assuming creation of an entity
                contentType : false,        // To force multipart/form-data
                data : fd,
                processData : false,
                success : function(data) {
                    // Handle the response on success
                    // alert(JSON.stringify(data));
                }
            });
        });
    });
</script>

这是回复:

    Remote Address:127.0.0.1:8080
Request URL:http://localhost:8080/api/upload/avatar
Request Method:POST
Status Code:422 Unprocessable Entity
Response Headers
view source
Access-Control-Allow-Origin:*
Access-Control-Expose-Headers:
Cache-Control:no-store, no-cache, must-revalidate, post-check=0, pre-check=0
Connection:Keep-Alive
Content-Length:202
Content-Type:application/problem+json
Date:Sun, 13 Sep 2015 15:14:27 GMT
Expires:Thu, 19 Nov 1981 08:52:00 GMT
Keep-Alive:timeout=5, max=94
Pragma:no-cache
Server:Apache/2.2.22 (Ubuntu)
Set-Cookie:apigility_oauth2_doctrine_skeleton=lcgemufh1sag5ive1o5smh51e3; path=/
X-Powered-By:PHP/5.4.45-1+deb.sury.org~precise+1
Request Headers
view source
Accept:*/*
Accept-Encoding:gzip, deflate
Accept-Language:de-DE,de;q=0.8,en-US;q=0.6,en;q=0.4
Connection:keep-alive
Content-Length:355592
Content-Type:multipart/form-data; boundary=----WebKitFormBoundaryaXrmYN1JAB3ShBJa
Host:localhost:8080
Origin:http://localhost:3000
Referer:http://localhost:3000/inserate
User-Agent:Mozilla/5.0 (Windows NT 10.0; WOW64) AppleWebKit/537.36 (KHTML, like Gecko) Chrome/45.0.2454.85 Safari/537.36
X-FirePHP-Version:0.0.6
Request Payload
------WebKitFormBoundaryaXrmYN1JAB3ShBJa
Content-Disposition: form-data; name="title"

test
------WebKitFormBoundaryaXrmYN1JAB3ShBJa
Content-Disposition: form-data; name="file_attachment"; filename="_DSC2288.jpg"
Content-Type: image/jpeg


------WebKitFormBoundaryaXrmYN1JAB3ShBJa
Content-Disposition: form-data; name="submit"


------WebKitFormBoundaryaXrmYN1JAB3ShBJa--

【问题讨论】:

  • BUMP - 你找到解决办法了吗?

标签: multipartform-data laminas-api-tools


【解决方案1】:

enctype="multipart/form-data" 添加到您的表单标签中。 确保您的 "data/" 文件夹是可写的。 另外,检查你的 PHP 错误日志是否有警告消息"Invalid boundary..."

同一个例子我也有同样的问题。终于让它与 POST 和 PUT 方法一起使用,但现在我在使用 X-HTTP-Method-Override 的 PUT 时遇到了同样的问题。它只是不工作,看起来像 apigility 代码中的一个错误。

【讨论】:

    猜你喜欢
    • 2015-10-23
    • 1970-01-01
    • 1970-01-01
    • 2016-01-27
    • 2011-02-12
    • 1970-01-01
    • 1970-01-01
    • 1970-01-01
    • 2018-04-08
    相关资源
    最近更新 更多