使用Angular引发问题导入文件

时间:2019-04-17 07:08:45

标签: javascript angular primeng

我正在使用Angular中的文件导入功能。从邮递员,我可以正确地发出请求,但是从Angular,我遇到了一个错误。请纠正我我在做什么错。

角度代码(伍美金)

<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)

邮递员头

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'

浏览器请求标头是

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


------WebKitFormBoundaryjZxBpNsIzAmLi5Sy--

1 个答案:

答案 0 :(得分:1)

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