cyclosproject / ng-swagger-gen

A Swagger 2.0 codegen for Angular
MIT License
221 stars 99 forks source link

Multipart/formdata file handling is not working correctly due FormData.append issue #205

Open dmushkov opened 4 years ago

dmushkov commented 4 years ago

There is a problem with generated code in case of using Multipart/Formdata There is a need generated code to be in form updateUsingPOSTResponse(params: DefinitionMetadataResourceService.UpdateDefinitionUsingPOSTParams): __Observable<__StrictHttpResponse<Array<MetadataDTO>>> { let __params = this.newParams(); let __headers = new HttpHeaders(); let __body: any = null; __headers.append('Content-Type', 'multipart/form-data'); let __formData = new FormData(); __body = __formData; if(params.definitionId !== null && typeof params.definitionId !== "undefined") { __formData.append('definitionId', params.definitionId as string | Blob);} if(params.tableResources !== null && typeof params.tableResources !== "undefined") { __formData.append('tableResources', params.tableResources as string | Blob, this is missing -> params.name);} if(params.fileResources !== null && typeof params.fileResources !== "undefined") { __formData.append('fileResources', params.fileResources as string | Blob, this is missing -> params.name);} let req = new HttpRequest<any>( 'POST', this.rootUrl +/api/definitions/update, __body, { headers: __headers, params: __params, responseType: 'json' }); return this.http.request<any>(req).pipe( __filter(_r => _r instanceof HttpResponse), __map((_r) => { return _r as __StrictHttpResponse<Array<MetadataDTO>>; }) ); }

so basically we need 3rd param to formdata.append in order code to work in IE

more about issue https://developer.mozilla.org/en-US/docs/Web/API/FormData/append section for IE notes With the "Include local directory pass when uploading files to a server" option enabled, IE will change the filename inside the Blob on the fly. To have direct control of the sent filename, the developer should send the filename as the third parameter value, i.e. formData.append(name, value, filename).

luisfpg commented 4 years ago

Which version of IE fails? According to http://researchhubs.com/post/computing/javascript/upload-files-with-ajax.html IE10 should work with all FormData methods. And the only supported version of IE is 11, so, it shouldn't be needed.