mirror of
https://github.com/cesanta/mongoose.git
synced 2025-08-06 13:37:34 +08:00
Add ajax_upload to big_upload
PUBLISHED_FROM=7c919ba3a2afa74cec9947e78299b4bb7a64b4bc
This commit is contained in:
parent
066be0fe30
commit
aab857a66e
45
examples/big_upload/ajax_upload_demo/index.html
Normal file
45
examples/big_upload/ajax_upload_demo/index.html
Normal file
@ -0,0 +1,45 @@
|
|||||||
|
<!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>
|
@ -10,6 +10,7 @@
|
|||||||
#include "mongoose.h"
|
#include "mongoose.h"
|
||||||
|
|
||||||
static const char *s_http_port = "8000";
|
static const char *s_http_port = "8000";
|
||||||
|
static struct mg_serve_http_opts s_http_server_opts;
|
||||||
|
|
||||||
struct file_writer_data {
|
struct file_writer_data {
|
||||||
FILE *fp;
|
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) {
|
static void ev_handler(struct mg_connection *nc, int ev, void *ev_data) {
|
||||||
(void) ev_data;
|
(void) ev_data;
|
||||||
switch (ev) {
|
if (ev == MG_EV_HTTP_REQUEST) {
|
||||||
case MG_EV_HTTP_REQUEST:
|
mg_printf(nc, "%s",
|
||||||
// Invoked when the full HTTP request is in the buffer (including body).
|
"HTTP/1.1 200 OK\r\n"
|
||||||
handle_request(nc);
|
"Content-Type: text/html\r\n"
|
||||||
break;
|
"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);
|
mg_mgr_init(&mgr, NULL);
|
||||||
nc = mg_bind(&mgr, s_http_port, ev_handler);
|
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, "/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(nc);
|
mg_set_protocol_http_websocket(nc);
|
||||||
|
|
||||||
|
Loading…
Reference in New Issue
Block a user