diff --git a/mongoose.c b/mongoose.c
index d11756499cda10e2b3896ebc0f7186455fed31e2..d09345c5ea54514447bc222648d601caad1450a7 100644
--- a/mongoose.c
+++ b/mongoose.c
@@ -8328,7 +8328,7 @@ void mg_file_upload_handler(struct mg_connection *nc, int ev, void *ev_data,
       if (lfn.p != mp->file_name) MG_FREE((char *) lfn.p);
       LOG(LL_DEBUG,
           ("%p Receiving file %s -> %s", nc, mp->file_name, fus->lfn));
-      fus->fp = mg_fopen(fus->lfn, "w");
+      fus->fp = mg_fopen(fus->lfn, "wb");
       if (fus->fp == NULL) {
         mg_printf(nc,
                   "HTTP/1.1 500 Internal Server Error\r\n"
diff --git a/src/mg_http.c b/src/mg_http.c
index 257bde4cebb68893a3110194879204f07e480c05..dcf4c22da569ae3a3db8dd963141ec9075da3df1 100644
--- a/src/mg_http.c
+++ b/src/mg_http.c
@@ -2737,7 +2737,7 @@ void mg_file_upload_handler(struct mg_connection *nc, int ev, void *ev_data,
       if (lfn.p != mp->file_name) MG_FREE((char *) lfn.p);
       LOG(LL_DEBUG,
           ("%p Receiving file %s -> %s", nc, mp->file_name, fus->lfn));
-      fus->fp = mg_fopen(fus->lfn, "w");
+      fus->fp = mg_fopen(fus->lfn, "wb");
       if (fus->fp == NULL) {
         mg_printf(nc,
                   "HTTP/1.1 500 Internal Server Error\r\n"