Skip to content
Projects
Groups
Snippets
Help
Loading...
Help
Contribute to GitLab
Sign in
Toggle navigation
M
mongoose
Project
Project
Details
Activity
Cycle Analytics
Repository
Repository
Files
Commits
Branches
Tags
Contributors
Graph
Compare
Charts
Issues
0
Issues
0
List
Board
Labels
Milestones
Merge Requests
0
Merge Requests
0
CI / CD
CI / CD
Pipelines
Jobs
Schedules
Charts
Wiki
Wiki
Members
Members
Collapse sidebar
Close sidebar
Activity
Graph
Charts
Create a new issue
Jobs
Commits
Issue Boards
Open sidebar
esp
mongoose
Commits
aab857a6
Commit
aab857a6
authored
8 years ago
by
Alexander Alashkin
Committed by
Cesanta Bot
8 years ago
Browse files
Options
Browse Files
Download
Email Patches
Plain Diff
Add ajax_upload to big_upload
PUBLISHED_FROM=7c919ba3a2afa74cec9947e78299b4bb7a64b4bc
parent
066be0fe
Changes
2
Hide whitespace changes
Inline
Side-by-side
Showing
2 changed files
with
80 additions
and
5 deletions
+80
-5
index.html
examples/big_upload/ajax_upload_demo/index.html
+45
-0
big_upload.c
examples/big_upload/big_upload.c
+35
-5
No files found.
examples/big_upload/ajax_upload_demo/index.html
0 → 100644
View file @
aab857a6
<!DOCTYPE html>
<html>
<head>
<title>
AJAX Upload Example
</title>
<script
src=
"//code.jquery.com/jquery-1.9.1.js"
></script>
<script
type=
"text/javascript"
>
function
updateProgress
(
evt
)
{
if
(
evt
.
lengthComputable
)
{
document
.
getElementById
(
"output"
).
textContent
=
"Uploaded "
+
evt
.
loaded
+
" of "
+
evt
.
total
+
" bytes"
;
}
}
function
uploadFile
()
{
var
file_data
=
new
FormData
(
document
.
getElementById
(
'filename'
));
$
.
ajax
({
url
:
"/upload"
,
type
:
"POST"
,
data
:
file_data
,
processData
:
false
,
contentType
:
false
,
cache
:
false
,
xhr
:
function
()
{
myXhr
=
$
.
ajaxSettings
.
xhr
();
if
(
myXhr
.
upload
){
myXhr
.
upload
.
addEventListener
(
'progress'
,
updateProgress
,
false
);
// for handling the progress of the upload
}
return
myXhr
;
},
}).
done
(
function
(
data
)
{
document
.
getElementById
(
"output"
).
textContent
=
"Result: "
+
data
;
});
return
false
;
}
</script>
</head>
<body>
<form
method=
"post"
id=
"filename"
name=
"filename"
onsubmit=
"return uploadFile();"
>
<label>
Select a file:
</label><br>
<input
type=
"file"
id=
"file"
name=
"file"
required
/>
<input
type=
"submit"
value=
"Upload"
/>
</form>
<br><br><div
id=
"output"
></div>
</body>
</html>
This diff is collapsed.
Click to expand it.
examples/big_upload/big_upload.c
View file @
aab857a6
...
...
@@ -10,6 +10,7 @@
#include "mongoose.h"
static
const
char
*
s_http_port
=
"8000"
;
static
struct
mg_serve_http_opts
s_http_server_opts
;
struct
file_writer_data
{
FILE
*
fp
;
...
...
@@ -83,11 +84,33 @@ 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
)
{
(
void
)
ev_data
;
switch
(
ev
)
{
case
MG_EV_HTTP_REQUEST
:
// Invoked when the full HTTP request is in the buffer (including body).
handle_request
(
nc
);
break
;
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
)
{
(
void
)
p
;
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
);
}
}
...
...
@@ -98,7 +121,14 @@ int main(void) {
mg_mgr_init
(
&
mgr
,
NULL
);
nc
=
mg_bind
(
&
mgr
,
s_http_port
,
ev_handler
);
s_http_server_opts
.
document_root
=
"."
;
// Serve current directory
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
mg_set_protocol_http_websocket
(
nc
);
...
...
This diff is collapsed.
Click to expand it.
Write
Preview
Markdown
is supported
0%
Try again
or
attach a new file
Attach a file
Cancel
You are about to add
0
people
to the discussion. Proceed with caution.
Finish editing this message first!
Cancel
Please
register
or
sign in
to comment