Skip to content
GitLab
Explore
Sign in
Register
Primary navigation
Search or go to…
Project
M
mongoose
Manage
Activity
Members
Labels
Plan
Issues
Issue boards
Milestones
Wiki
Code
Merge requests
Repository
Branches
Commits
Tags
Repository graph
Compare revisions
Build
Pipelines
Jobs
Pipeline schedules
Artifacts
Deploy
Releases
Package registry
Container Registry
Model registry
Operate
Environments
Terraform modules
Monitor
Incidents
Analyze
Value stream analytics
Contributor analytics
CI/CD analytics
Repository analytics
Model experiments
Help
Help
Support
GitLab documentation
Compare GitLab plans
Community forum
Contribute to GitLab
Provide feedback
Keyboard shortcuts
?
Snippets
Groups
Projects
Show more breadcrumbs
Ganil-acq
GANILinux
linux-service
library
mongoose
Commits
ceb2f1da
Commit
ceb2f1da
authored
8 years ago
by
Sergey Lyubka
Committed by
Cesanta Bot
8 years ago
Browse files
Options
Downloads
Patches
Plain Diff
Update upload example
PUBLISHED_FROM=a28c1da3f0dd597792439246313ca39984ab7536
parent
aab857a6
No related branches found
No related tags found
No related merge requests found
Changes
2
Hide whitespace changes
Inline
Side-by-side
Showing
2 changed files
examples/big_upload/big_upload.c
+9
-52
9 additions, 52 deletions
examples/big_upload/big_upload.c
examples/big_upload/index.html
+9
-0
9 additions, 0 deletions
examples/big_upload/index.html
with
18 additions
and
52 deletions
examples/big_upload/big_upload.c
+
9
−
52
View file @
ceb2f1da
...
@@ -17,22 +17,6 @@ struct file_writer_data {
...
@@ -17,22 +17,6 @@ struct file_writer_data {
size_t
bytes_written
;
size_t
bytes_written
;
};
};
static
void
handle_request
(
struct
mg_connection
*
nc
)
{
// This handler gets for all endpoints but /upload
mg_printf
(
nc
,
"%s"
,
"HTTP/1.1 200 OK
\r\n
"
"Content-Type: text/html
\r\n
"
"Connection: close
\r\n
"
"
\r\n
"
"<html><body>Upload example."
"<form method=
\"
POST
\"
action=
\"
/upload
\"
"
" enctype=
\"
multipart/form-data
\"
>"
"<input type=
\"
file
\"
name=
\"
file
\"
/> <br/>"
"<input type=
\"
submit
\"
value=
\"
Upload
\"
/>"
"</form></body></html>"
);
nc
->
flags
|=
MG_F_SEND_AND_CLOSE
;
}
static
void
handle_upload
(
struct
mg_connection
*
nc
,
int
ev
,
void
*
p
)
{
static
void
handle_upload
(
struct
mg_connection
*
nc
,
int
ev
,
void
*
p
)
{
struct
file_writer_data
*
data
=
(
struct
file_writer_data
*
)
nc
->
user_data
;
struct
file_writer_data
*
data
=
(
struct
file_writer_data
*
)
nc
->
user_data
;
struct
mg_http_multipart_part
*
mp
=
(
struct
mg_http_multipart_part
*
)
p
;
struct
mg_http_multipart_part
*
mp
=
(
struct
mg_http_multipart_part
*
)
p
;
...
@@ -83,54 +67,27 @@ static void handle_upload(struct mg_connection *nc, int ev, void *p) {
...
@@ -83,54 +67,27 @@ static void handle_upload(struct mg_connection *nc, int ev, void *p) {
}
}
static
void
ev_handler
(
struct
mg_connection
*
nc
,
int
ev
,
void
*
ev_data
)
{
static
void
ev_handler
(
struct
mg_connection
*
nc
,
int
ev
,
void
*
ev_data
)
{
(
void
)
ev_data
;
if
(
ev
==
MG_EV_HTTP_REQUEST
)
{
mg_printf
(
nc
,
"%s"
,
"HTTP/1.1 200 OK
\r\n
"
"Content-Type: text/html
\r\n
"
"Connection: close
\r\n
"
"
\r\n
"
"<html><body>Upload example."
"Navigate to <a "
"href=
\"
simple_upload_demo
\"
>/simple_upload_demo</a> for "
"uploading using submit "
"or to <a href=
\"
/ajax_upload_demo
\"
>/ajax_upload_demo</a> for "
"uploading using ajax"
"</form></body></html>"
);
nc
->
flags
|=
MG_F_SEND_AND_CLOSE
;
}
}
static
void
upload_demo_handler
(
struct
mg_connection
*
nc
,
int
ev
,
void
*
p
)
{
if
(
ev
==
MG_EV_HTTP_REQUEST
)
{
if
(
ev
==
MG_EV_HTTP_REQUEST
)
{
(
void
)
p
;
mg_serve_http
(
nc
,
ev_data
,
s_http_server_opts
);
handle_request
(
nc
);
}
}
static
void
ajax_demo_handler
(
struct
mg_connection
*
nc
,
int
ev
,
void
*
p
)
{
if
(
ev
==
MG_EV_HTTP_REQUEST
)
{
mg_serve_http
(
nc
,
(
struct
http_message
*
)
p
,
s_http_server_opts
);
}
}
}
}
int
main
(
void
)
{
int
main
(
void
)
{
struct
mg_mgr
mgr
;
struct
mg_mgr
mgr
;
struct
mg_connection
*
n
c
;
struct
mg_connection
*
c
;
mg_mgr_init
(
&
mgr
,
NULL
);
mg_mgr_init
(
&
mgr
,
NULL
);
nc
=
mg_bind
(
&
mgr
,
s_http_port
,
ev_handler
);
c
=
mg_bind
(
&
mgr
,
s_http_port
,
ev_handler
);
if
(
c
==
NULL
)
{
fprintf
(
stderr
,
"Cannot start server on port %s
\n
"
,
s_http_port
);
exit
(
EXIT_FAILURE
);
}
s_http_server_opts
.
document_root
=
"."
;
// Serve current directory
s_http_server_opts
.
document_root
=
"."
;
// Serve current directory
mg_register_http_endpoint
(
c
,
"/upload"
,
handle_upload
MG_UD_ARG
(
NULL
));
mg_register_http_endpoint
(
nc
,
"/upload"
,
handle_upload
MG_UD_ARG
(
NULL
));
mg_register_http_endpoint
(
nc
,
"/ajax_upload_demo"
,
ajax_demo_handler
MG_UD_ARG
(
NULL
));
mg_register_http_endpoint
(
nc
,
"/simple_upload_demo"
,
upload_demo_handler
MG_UD_ARG
(
NULL
));
// Set up HTTP server parameters
// Set up HTTP server parameters
mg_set_protocol_http_websocket
(
n
c
);
mg_set_protocol_http_websocket
(
c
);
printf
(
"Starting web server on port %s
\n
"
,
s_http_port
);
printf
(
"Starting web server on port %s
\n
"
,
s_http_port
);
for
(;;)
{
for
(;;)
{
...
...
This diff is collapsed.
Click to expand it.
examples/big_upload/
ajax_upload_demo/
index.html
→
examples/big_upload/index.html
+
9
−
0
View file @
ceb2f1da
...
@@ -35,6 +35,15 @@
...
@@ -35,6 +35,15 @@
</head>
</head>
<body>
<body>
<h1>
Upload file using standard form upload
</h1>
<form
method=
"POST"
action=
"/upload"
enctype=
"multipart/form-data"
>
<label>
Select a file:
</label><br>
<input
type=
"file"
name=
"file"
/>
<input
type=
"submit"
value=
"Upload"
/>
</form>
<h1>
Upload file using Ajax - that also gives progress report
</h1>
<form
method=
"post"
id=
"filename"
name=
"filename"
onsubmit=
"return uploadFile();"
>
<form
method=
"post"
id=
"filename"
name=
"filename"
onsubmit=
"return uploadFile();"
>
<label>
Select a file:
</label><br>
<label>
Select a file:
</label><br>
<input
type=
"file"
id=
"file"
name=
"file"
required
/>
<input
type=
"file"
id=
"file"
name=
"file"
required
/>
...
...
This diff is collapsed.
Click to expand it.
Preview
0%
Loading
Try again
or
attach a new file
.
Cancel
You are about to add
0
people
to the discussion. Proceed with caution.
Finish editing this message first!
Save comment
Cancel
Please
register
or
sign in
to comment