Using Multiple inputs for upload example

This commit is contained in:
Sergey Lyubka 2014-06-24 16:35:59 +01:00
parent ca9603301e
commit 6e07215c1c

View File

@ -5,9 +5,9 @@
#include <string.h> #include <string.h>
#include "mongoose.h" #include "mongoose.h"
static void send_index_page(struct mg_connection *conn) { static int send_index_page(struct mg_connection *conn) {
const char *data; const char *data;
int data_len; int data_len, ofs = 0;
char var_name[100], file_name[100]; char var_name[100], file_name[100];
mg_printf_data(conn, "%s", mg_printf_data(conn, "%s",
@ -15,30 +15,28 @@ static void send_index_page(struct mg_connection *conn) {
"<form method=\"POST\" action=\"/handle_post_request\" " "<form method=\"POST\" action=\"/handle_post_request\" "
" enctype=\"multipart/form-data\">" " enctype=\"multipart/form-data\">"
"<input type=\"file\" name=\"file\" /> <br/>" "<input type=\"file\" name=\"file\" /> <br/>"
"<input type=\"text\" name=\"input1\" value=hello! /> <br/>"
"<input type=\"submit\" value=\"Upload\" />" "<input type=\"submit\" value=\"Upload\" />"
"</form>"); "</form>");
if (mg_parse_multipart(conn->content, conn->content_len, while ((ofs = mg_parse_multipart(conn->content + ofs, conn->content_len - ofs,
var_name, sizeof(var_name), var_name, sizeof(var_name),
file_name, sizeof(file_name), file_name, sizeof(file_name),
&data, &data_len) > 0) { &data, &data_len)) > 0) {
mg_printf_data(conn, "var: %s, file_name: %s, size: %d bytes<br>",
mg_printf_data(conn, "%s", "Uploaded file:<pre>"); var_name, file_name, data_len);
mg_send_data(conn, data, data_len);
mg_printf_data(conn, "%s", "/pre>");
} }
mg_printf_data(conn, "%s", "</body></html>"); mg_printf_data(conn, "%s", "</body></html>");
return MG_TRUE;
} }
static int ev_handler(struct mg_connection *conn, enum mg_event ev) { static int ev_handler(struct mg_connection *conn, enum mg_event ev) {
if (ev == MG_REQUEST) { switch (ev) {
send_index_page(conn); case MG_AUTH: return MG_TRUE;
return MG_TRUE; case MG_REQUEST: return send_index_page(conn);
} else if (ev == MG_AUTH) { default: return MG_FALSE;
return MG_TRUE;
} else {
return MG_FALSE;
} }
} }