python open(filepath, 'rb') in JavaScript 的等价物是什么

what is the equivalent of python open(filepath, 'rb') in JavaScript

我可以使用 Python 以二进制模式打开文件并将其存储在变量中。 我已经研究了两天了,似乎找不到直接的答案。我需要这个,因为我一直在做一个我们在 Python 完成的项目,但我们需要将它转换成 JavaScript。我遇到的问题是我需要通过 API 调用发送文件。尽管如此,每次我这样做时,我都会收到此错误 ' HTTP 415 Unsupported Media TypeThe provided media type is not supported for the requested URI。我有一个 JSON 文件,我需要将其传递给 post-二进制调用

files = {'file': open(JSON_File, 'rb')}
response = requests.post(
    URL, 
    auth=(USERNAME, PASSWORD),
    files=files
    #files=files, verify = False
)

response.text

我如何在 nodeJS 中执行此操作?我已经在 Python.

完成了以上内容

下面是 files 变量从 python 代码打印出来的内容。

files = {'file': open(JSON_File_path, 'rb')}

print(files) => {'file': <_io.BufferedReader name='my_json_file.json'>}

这是我的 json 文件包含的示例:

    [
  {
    "resourceType": "Domain",
    "identifier": {
      "name": "ZZ_CRM",
      "community": {
        "name": "Intermediate APIs"
      }
    },
    "type": {
      "name": "Data Asset Domain"
    }
  },
  {
    "resourceType": "Domain",
    "identifier": {
      "name": "ZZ_Source to Target Mappings",
      "community": {
        "name": "Intermediate APIs"
      }
    },
    "type": {
      "name": "Mapping Domain"
    }
  },
  {
    "resourceType": "Asset",
    "identifier": {
      "name": "Client Name",
      "domain": {
        "name": "ZZ_CRM",
        "community": {
          "name": "Intermediate APIs"
        }
      }
    },
    "type": {
      "name": "Data Element"
    }
  },
  {
    "resourceType": "Asset",
    "identifier": {
      "name": "Client ID",
      "domain": {
        "name": "ZZ_CRM",
        "community": {
          "name": "Intermediate APIs"
        }
      }
    },
    "type": {
      "name": "Data Element"
    }
  },
  {
    "resourceType": "Asset",
    "identifier": {
      "name": "SQL Server Mapping",
      "domain": {
        "name": "ZZ_Source to Target Mappings",
        "community": {
          "name": "Intermediate APIs"
        }
      }
    },
    "type": {
      "name": "Mapping Specification"
    }
  },
  {
    "resourceType": "Complex Relation",
    "identifier": {
      "relations": {
        "00000000-0000-0000-0000-000000007088:TARGET": [
          {
            "name": "SQL Server Mapping1Name",
            "domain": {
              "name": "ZZ_Source to Target Mappings1Name",
              "community": {
                "name": "Intermediate APIs1Name"
              }
            }
          }
        ],
        "00000000-0000-0000-0000-000000007089:TARGET": [
          {
            "name": "Client ID",
            "domain": {
              "name": "ZZ_CRM",
              "community": {
                "name": "Intermediate APIs"
              }
            }
          }
        ],
        "00000000-0000-0000-0000-000000007090:TARGET": [
          {
            "name": "SQL Server Mapping",
            "domain": {
              "name": "ZZ_Source to Target Mappings",
              "community": {
                "name": "Intermediate APIs"
              }
            }
          }
        ]
      }
    },
    "complexRelationType": {
      "name": "Field Mapping"
    },
    "attributes": {
      "Transformation Logic": [
        {
          "value": "`clientsWithType` AS select `client`.`client_id` AS `id`, concat( concat( `client`.`name`, ' ' ), `client`.`surname` ) AS `fullname`, `client_type`.`value` AS `client_type`, `client`.`email_address` AS `email_address` from ( `client` join `client_type` on (( `client`.`client_type` = `client_type`.`code` )));"
        }
      ]
    }
  }
]

请试试这个,

https://nodejs.org/api/fs.html#fs_fs_readfile_file_options_callback

import { open } from 'fs/promises';
let filehandle = null;
try {
  filehandle = await open(<filepath>, <options>);
}