使用javascript swagger客户端进行文件上传的麻烦并做出反应

问题描述 投票:0回答:1

我对Swagger codegen生成的源代码有疑问。我想上传一个带有反应的文件。为此,我创建了一个Dropzone并获取该文件的路径。如果我在文档中使用生成的客户端,它将无法正常工作。不幸的是,该文件未发送。只有文件名。调试控制台也不显示已发送二进制数据。

请求未正确执行。该文件将不会上传。参数“file”只是文件名,而不是二进制数据。

Swagger-codegen版本

OpenAPI的发电机-CLI-3.3.4.jar

Swagger声明文件内容

Swagger .yaml:

  /orders/upload:
    post:
      operationId: "orderUploadPart"
      summary: "upload a textual or audio part of an order"
      tags:
        - "orders"
      description: "This funktion uploads a textual or audio part of an order to the sense.ai.tion cloud system. 
      The result is the resource identifier, that must be used in the order request."
      consumes:
        - multipart/form-data
      parameters:
        - in: "formData"
          name: "file"
          type: "file"
          required: true
          description: "the file to upload"
        - in: "formData"
          name: "media"
          type: "string"
          enum:
            - "text"
            - "wav"
            - "mp3"
          required: true
          description: "the media type of the the upload, can be ***text***, ***wav*** or ***mp3***"

码:

var apiInstance = new SenseaitionApi.OrdersApi();
var file = "/path/to/file"; // File | the file to upload
var media = "media_example"; // String | the media type of the the upload, can be ***text***, ***wav*** or ***mp3***
var callback = function(error, data, response) {
  if (error) {
    console.error(error);
  } else {
    console.log('API called successfully. Returned data: ' + data);
  }
};
apiInstance.orderUploadPart(file, media, callback);

就像在:https://github.com/swagger-api/swagger-codegen/blob/master/samples/client/petstore/javascript/docs/PetApi.md#uploadFile

Screenshot Chrome DevTools

用于生成的命令行

java -jar ${GOPATH}/bin/openapi-generator-cli.jar generate -i service_js_api.yaml -g javascript -o clients/javascript/senseaition-api-js -Dio.swagger.parser.util.RemoteUrl.trustAll=true

javascript reactjs swagger swagger-codegen react-dropzone
1个回答
0
投票

我发现了错误。生成的Javascript代码的文档是错误的。对于上传文件(Javascript对象)必须传递,而不是Path。

这条线错了:

var file = "/path/to/file"; // File | the file to upload 
© www.soinside.com 2019 - 2024. All rights reserved.