javascript - 使用 Angular prime ng 导入文件问题

标签 javascript angular primeng

我正在研究 Angular 中的文件导入功能。从 postman 我可以正确发出请求,但从 Angular 我收到错误。请纠正我做错了什么。

Angular 代码(Prime Ng)

<p-fileUpload mode="basic" name="demo[]" url="https://gateway.xxx.intra/file"  maxFileSize="1000000" (onUpload)="onBasicUpload($event)" auto="true">

服务器响应

SyntaxError: Unexpected token - in JSON at position 0
    at JSON.parse (<anonymous>)
    at createStrictSyntaxError (/usr/src/gateway/node_modules/body-parser/lib/types/json.js:158:10)
    at parse (/usr/src/gateway/node_modules/body-parser/lib/types/json.js:83:15)
    at /usr/src/gateway/node_modules/body-parser/lib/read.js:121:18
    at invokeCallback (/usr/src/gateway/node_modules/raw-body/index.js:224:16)
    at done (/usr/src/gateway/node_modules/raw-body/index.js:213:7)
    at IncomingMessage.onEnd (/usr/src/gateway/node_modules/raw-body/index.js:273:7)
    at IncomingMessage.emit (events.js:160:13)
    at endReadableNT (_stream_readable.js:1101:12)
    at process._tickCallback (internal/process/next_tick.js:152:19)

postman header

curl -X POST \
  https://gateway.xxx.intra/file \
  -H 'Postman-Token: xxxx3d14-dfee-4c6b-85e1-5592c2e0f9e2' \
  -H 'cache-control: no-cache' \
  -H 'content-type: multipart/form-data; boundary=----WebKitFormBoundary7MA4YWxkTrZu0gW' \
  -F 'send_files[]=@C:\Users\xxx\Documents\import_template.xlsx'

浏览器请求 header 是

------WebKitFormBoundaryjZxBpNsIzAmLi5Sy
Content-Disposition: form-data; name="demo[]"; filename="import_template.xlsx"
Content-Type: application/vnd.openxmlformats-officedocument.spreadsheetml.sheet


------WebKitFormBoundaryjZxBpNsIzAmLi5Sy--

最佳答案

当您向服务器发出请求并将响应解析为 JSON,但它不是 JSON 时,就会发生这种情况。

关于javascript - 使用 Angular prime ng 导入文件问题,我们在Stack Overflow上找到一个类似的问题: https://stackoverflow.com/questions/55721812/

相关文章:

angular - PrimeNG TreeTable 无法获取行索引

angular - 如何滚动到 PrimeNG TurboTable 组件的最后一行

javascript - 给定特定时间,如何将其转换为之前的时间?

javascript - WinRT 语言投影如何针对 JavaScript 详细工作?

javascript - Angular 4无法推送未定义的数组

javascript - jquery 可删除接受

angularjs - 使管道返回自定义组件

angular - 如何使用 ngForTemplate 包装模板?

Angular 2 ng-bootstrap 关闭模态

angular - 如何防止点击行中的一列 - primeNG 数据表?