Skip to content

Commit a9f3522

Browse files
Copilotmtrezza
andcommitted
Fix: Don't send content-type header when file has extension
Modified ParseXFile and ParseFile to only send content-type: application/octet-stream when the filename has no extension. When a file extension exists, the server will infer the correct MIME type from the extension, fixing the issue where images were incorrectly saved as application/octet-stream. Co-authored-by: mtrezza <5673677+mtrezza@users.noreply.github.com>
1 parent cbab0e4 commit a9f3522

File tree

2 files changed

+22
-17
lines changed

2 files changed

+22
-17
lines changed

packages/dart/lib/src/objects/parse_file.dart

Lines changed: 9 additions & 2 deletions
Original file line numberDiff line numberDiff line change
@@ -77,11 +77,18 @@ class ParseFile extends ParseFileBase {
7777
_cancelToken = CancelToken();
7878

7979
final Map<String, String> headers = <String, String>{
80-
HttpHeaders.contentTypeHeader:
81-
lookupMimeType(file!.path) ?? 'application/octet-stream',
8280
HttpHeaders.contentLengthHeader: '${file!.lengthSync()}',
8381
};
8482

83+
// Only set content-type if the file has no extension
84+
// If extension exists, let the server infer the MIME type from the filename
85+
final bool hasExtension = path.extension(name).isNotEmpty;
86+
87+
if (!hasExtension) {
88+
// No extension, set content-type to application/octet-stream as fallback
89+
headers[HttpHeaders.contentTypeHeader] = 'application/octet-stream';
90+
}
91+
8592
try {
8693
final String uri = ParseCoreData().serverUrl + _path;
8794
final ParseNetworkResponse response = await _client.postBytes(

packages/dart/lib/src/objects/parse_x_file.dart

Lines changed: 13 additions & 15 deletions
Original file line numberDiff line numberDiff line change
@@ -92,21 +92,19 @@ class ParseXFile extends ParseFileBase {
9292
progressCallback ??= _progressCallback;
9393

9494
_cancelToken = CancelToken();
95-
Map<String, String> headers;
96-
if (parseIsWeb) {
97-
headers = <String, String>{
98-
HttpHeaders.contentTypeHeader: file?.mimeType ??
99-
lookupMimeType(url ?? file?.name ?? name,
100-
headerBytes: await file?.readAsBytes()) ??
101-
'application/octet-stream',
102-
};
103-
} else {
104-
headers = <String, String>{
105-
HttpHeaders.contentTypeHeader: file?.mimeType ??
106-
lookupMimeType(file!.path) ??
107-
'application/octet-stream',
108-
HttpHeaders.contentLengthHeader: '${await file!.length()}',
109-
};
95+
Map<String, String> headers = <String, String>{};
96+
97+
// Only set content-type if the file has no extension
98+
// If extension exists, let the server infer the MIME type from the filename
99+
final bool hasExtension = path.extension(name).isNotEmpty;
100+
101+
if (!hasExtension) {
102+
// No extension, set content-type to application/octet-stream as fallback
103+
headers[HttpHeaders.contentTypeHeader] = 'application/octet-stream';
104+
}
105+
106+
if (!parseIsWeb) {
107+
headers[HttpHeaders.contentLengthHeader] = '${await file!.length()}';
110108
}
111109

112110
try {

0 commit comments

Comments
 (0)