diff --git a/packages/react-native/Libraries/Network/FormData.js b/packages/react-native/Libraries/Network/FormData.js index 91735c6b033b19..b237b45ae24344 100644 --- a/packages/react-native/Libraries/Network/FormData.js +++ b/packages/react-native/Libraries/Network/FormData.js @@ -28,6 +28,15 @@ type FormDataPart = ... }; +/** + * Encode a FormData filename compliant with RFC 2183 + * + * https://developer.mozilla.org/en-US/docs/Web/HTTP/Headers/Content-Disposition#directives + */ +function encodeFilename(filename: string): string { + return encodeURIComponent(filename.replace(/\//g, '_')); +} + /** * Polyfill for XMLHttpRequest2 FormData API, allowing multipart POST requests * with mixed data (string, native files) to be submitted via XMLHttpRequest. @@ -82,9 +91,8 @@ class FormData { // content type (cf. web Blob interface.) if (typeof value === 'object' && !Array.isArray(value) && value) { if (typeof value.name === 'string') { - headers['content-disposition'] += `; filename="${ - value.name - }"; filename*=utf-8''${encodeURI(value.name)}`; + headers['content-disposition'] += + `; filename="${encodeFilename(value.name)}"`; } if (typeof value.type === 'string') { headers['content-type'] = value.type; diff --git a/packages/react-native/Libraries/Network/__tests__/FormData-test.js b/packages/react-native/Libraries/Network/__tests__/FormData-test.js index ee741afb7c983b..18830d5f15f3e0 100644 --- a/packages/react-native/Libraries/Network/__tests__/FormData-test.js +++ b/packages/react-native/Libraries/Network/__tests__/FormData-test.js @@ -48,8 +48,7 @@ describe('FormData', function () { type: 'image/jpeg', name: 'photo.jpg', headers: { - 'content-disposition': - 'form-data; name="photo"; filename="photo.jpg"; filename*=utf-8\'\'photo.jpg', + 'content-disposition': 'form-data; name="photo"; filename="photo.jpg"', 'content-type': 'image/jpeg', }, fieldName: 'photo', @@ -70,7 +69,7 @@ describe('FormData', function () { name: '测试photo.jpg', headers: { 'content-disposition': - 'form-data; name="photo"; filename="测试photo.jpg"; filename*=utf-8\'\'%E6%B5%8B%E8%AF%95photo.jpg', + 'form-data; name="photo"; filename="%E6%B5%8B%E8%AF%95photo.jpg"', 'content-type': 'image/jpeg', }, fieldName: 'photo',