cancel
Showing results for 
Search instead for 
Did you mean: 

Uploading files via gateway refused if content-type header not set

frankluzat
Participant
0 Kudos

Hi,

I have a media entity in my service which I use to upload files from my UI5 webapplication to the SAP ERP backend and connected content servers.

On the client I use a sap.m.UploadCollection control for the file upload. I don't know if it's the control or the browser which sets the content-type header for the POST request.

I have some file types like .mpp or .pages for which no MIME-type is determined and thus the content-type header is not transmitted.

Since gateway checks the submitted content-type it refuses to upload files which have not set the content-type header.

According to this discussion no content-type header should be submitted by the sender if the MIME type is unknown.

What can I do about it?

Why does gateway do this check and why does it refuse files without content-type header?

Accepted Solutions (1)

Accepted Solutions (1)

brian_zhu
Explorer
0 Kudos

Hi Frank,

As far as i know that the Content-type is the mandatory field you need you fill when doing upload file

in create stream method.

you could check this via the gateway client to see the values in the header.

there is another mandatory field you need to populate in the header is

the slug.

From the perspective of project experience, we have limited the scope of the files for *.jpg and *.pdf.. this is for your reference.

regards,

Yueqiang

frankluzat
Participant
0 Kudos

Hi Yueqiang,

I see from gateway code that content-type is mandatory, but I see no reason why.

If I step over the validation routine in the debugger, my file uploads fine.

For our project the range of file types is also limited but we have some more files to handle than only *.jpg and *.pdf.

Even trying to upload an MS Outlook *.msg file fails because of missing content-type.

On the UI5 side I could add content-type as application/unknown for every file which would make any upload work but this would also overwrite content-types like text/plain which had been determined correctly by the client.

brian_zhu
Explorer
0 Kudos

Hi Frank,

the content type is the MIME type in DB.

I used to have a case without the MIME type stored in the DB. then the JPG /PDF cand not be

displayed correctly. the uploaded files could written and read from DB.

regards,

Yueqiang

frankluzat
Participant
0 Kudos

According to SAP OSS support the gateway implementation adheres to the OData standard which says that content-type is a mandatory header field (at least for media link entries).

I solved this by adding the content-type: application/unknown header in the UI5 application if the no mime-type had been determined for a file.

EkanshCapgemini
Active Contributor
0 Kudos

Hi Frank,

As you have already figured out that content-type is mandatory to pass while uploading the file through the gateway service. Still there are following options that you can try :

1) you can use some third party APIs to detect the MIME type of a file in javascript. These APIs will figure out the MIME type by checking the binary signature of file. So there are fair amount of chances that you will get the correct MIME types for most of the common file types even when someone has changed the extension.

2) if you do not want to use these APIs then you have to rely on the file extensions and detect the MIME type either at client side or at server side using FM 'SDOK_MIMETYPE_GET'.

3) otherwise if you are still not able to find the proper MIME type, you can always set it as "application/octet-stream" in the response and use other headers to set the filename with extension. This way browser will download the file and try to open the file with the help of its extension. You can check this link for more details on this part. However I am quoting following from this link:


Content-Type: application/octet-stream Content-Disposition: attachment; filename="picture.png" 

Means "I don't know what the hell this is. Please save it as a file, preferably named picture.png".

Content-Type: image/png Content-Disposition: attachment; filename="picture.png" 

Means "This is a PNG image. Please save it as a file, preferably named picture.png".

Content-Type: image/png Content-Disposition: inline; filename="picture.png" 

Means "This is a PNG image. Please display it unless you don't know how to display PNG images. Otherwise, or if the user chooses to save it, we recommend the name picture.png for the file you save it as".

Regards,

Ekansh

Answers (0)