Skip to content

UtowFileUpload.getFileSize throws exception when "Content-Length" header is missing. #1650

@inzagher

Description

@inzagher

I've tried to upload file from angular 7.
Angular application code looks so:

public upload(id: string, name: string, type: string, data: File): Observable<any> {
    const form = new FormData();
    form.append("id", id);
    form.append("name", name);
    form.append("file", data, data.name);
    form.append("type", !!type ? type : "unknown");
    return this.http.post(this.url, form, { responseType: "text" as "json" });
}

Server side:

@POST("/attach")
@Consumes(MediaType.MULTIPART_FORMDATA)
@Produces(MediaType.TEXT)
public Object attach(Context context) throws Exception {
    FileUpload file = context.file("file");
    long contentSize = file.getFileSize();
    ...
}

When I'm trying to get file size exception is thrown:
java.lang.NumberFormatException: null
at java.lang.Long.parseLong(Long.java:552)
at java.lang.Long.parseLong(Long.java:631)
at io.jooby.internal.utow.UtowFileUpload.getFileSize(UtowFileUpload.java:62)

@Override public long getFileSize() {
    return Long.parseLong(upload.getHeaders().getFirst(Headers.CONTENT_LENGTH));
}

------WebKitFormBoundaryub4A7wV0aSpsIBd8
Content-Disposition: form-data; name="id"

3619f3d0-1aa7-4b41-baaa-f5431a597666
------WebKitFormBoundaryub4A7wV0aSpsIBd8
Content-Disposition: form-data; name="name"

Ping
------WebKitFormBoundaryub4A7wV0aSpsIBd8
Content-Disposition: form-data; name="file"; filename="Ping.xml"
Content-Type: text/xml

------WebKitFormBoundaryub4A7wV0aSpsIBd8
Content-Disposition: form-data; name="type"

unknown
------WebKitFormBoundaryub4A7wV0aSpsIBd8--

As we can see, form data does not contain "Content-Length" header for uploaded file.
I don't know is it bug or my mistake.

Metadata

Metadata

Assignees

No one assigned

    Type

    No type

    Projects

    No projects

    Milestone

    Relationships

    None yet

    Development

    No branches or pull requests

    Issue actions