javascript 上传带有Angular 的文件-预期数字(0-9)跟随减号错误

lo8azlld  于 2023-02-02  发布在  Java
关注(0)|答案(1)|浏览(103)

我试图上传文件与角代码,我得到这个错误:

Unexpected character ('-' (code 45)) in numeric value: expected digit (0-9) to follow minus sign, for valid numeric value
 at [Source: (org.glassfish.jersey.message.internal.ReaderInterceptorExecutor$UnCloseableInputStream); line: 1, column: 3]

当我用 Postman -

它起作用了。
Postman 的代码片段告诉我

var myHeaders = new Headers();
myHeaders.append("Content-Type", "application/json");

var formdata = new FormData();
formdata.append("details", "{}");
formdata.append("payload", fileInput.files[0], "/C:/Users/..../Downloads/service-3663419633761-csar.csar");

var requestOptions = {
  method: 'POST',
  headers: myHeaders,
  body: formdata,
  redirect: 'follow'
};

fetch("http://.../upload", requestOptions)
  .then(response => response.text())
  .then(result => console.log(result))
  .catch(error => console.log('error', error));

所以我用同样的方法来处理angular:

importCatalogEntityApi = (
    file: File
): IDeferred<IComponentsPagination> => {
        
    const defer = this.$q.defer<IComponentsPagination>();
    
    let formData:FormData = new FormData();
    formData.append("details", "{}");
    formData.append('payload', file, file.name);

    const options = {};

    this.$http
    .post("http://.../upload", formData, options)
    .then(
        (response: any) => {
            console.log("success: " + response)
            const defferObj = this.convertEntitiesResponse(response);
            defer.resolve(defferObj);
        },
        (response) => {
            console.log("failure: " + response)
            defer.reject(response);
        }
    );
    
    return defer;
};

我得到了上面的错误。有时我得到了这个错误:

org.apache.http.client.NonRepeatableRequestException: Cannot retry request with a non-repeatable request entity

过渡氮氧化合物

5gfr0r5j

5gfr0r5j1#

您发送的Content-Type标头似乎不正确,应该是multipart/form-data而不是application/json

相关问题