diff --git a/examples/upload.c b/examples/upload.c index 4073afb4..2737ba98 100644 --- a/examples/upload.c +++ b/examples/upload.c @@ -5,9 +5,9 @@ #include #include "mongoose.h" -static void send_index_page(struct mg_connection *conn) { +static int send_index_page(struct mg_connection *conn) { const char *data; - int data_len; + int data_len, ofs = 0; char var_name[100], file_name[100]; mg_printf_data(conn, "%s", @@ -15,30 +15,28 @@ static void send_index_page(struct mg_connection *conn) { "
" "
" + "
" "" "
"); - if (mg_parse_multipart(conn->content, conn->content_len, - var_name, sizeof(var_name), - file_name, sizeof(file_name), - &data, &data_len) > 0) { - - mg_printf_data(conn, "%s", "Uploaded file:
");
-    mg_send_data(conn, data, data_len);
-    mg_printf_data(conn, "%s", "/pre>");
+  while ((ofs = mg_parse_multipart(conn->content + ofs, conn->content_len - ofs,
+                                   var_name, sizeof(var_name),
+                                   file_name, sizeof(file_name),
+                                   &data, &data_len)) > 0) {
+    mg_printf_data(conn, "var: %s, file_name: %s, size: %d bytes
", + var_name, file_name, data_len); } mg_printf_data(conn, "%s", ""); + + return MG_TRUE; } static int ev_handler(struct mg_connection *conn, enum mg_event ev) { - if (ev == MG_REQUEST) { - send_index_page(conn); - return MG_TRUE; - } else if (ev == MG_AUTH) { - return MG_TRUE; - } else { - return MG_FALSE; + switch (ev) { + case MG_AUTH: return MG_TRUE; + case MG_REQUEST: return send_index_page(conn); + default: return MG_FALSE; } }