Commit aab857a6 authored by Alexander Alashkin's avatar Alexander Alashkin Committed by Cesanta Bot

Add ajax_upload to big_upload

PUBLISHED_FROM=7c919ba3a2afa74cec9947e78299b4bb7a64b4bc
parent 066be0fe
<!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"
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).
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);
break;
}
}
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);
......
Markdown is supported
0% or
You are about to add 0 people to the discussion. Proceed with caution.
Finish editing this message first!
Please register or to comment