From ec752645ee0cc3c7fd738ff6ac5b3b6f480fa97c Mon Sep 17 00:00:00 2001 From: Alexander Alashkin <alexander.alashkin@cesanta.com> Date: Tue, 21 Mar 2017 07:24:36 +0000 Subject: [PATCH] Remove ajax_upload PUBLISHED_FROM=599ccd99e7bf422cac5e8b1f5ccebe3cac2780e8 --- examples/ajax_upload/Makefile | 3 -- examples/ajax_upload/ajax_upload.c | 85 ------------------------------ examples/ajax_upload/index.html | 47 ----------------- 3 files changed, 135 deletions(-) delete mode 100644 examples/ajax_upload/Makefile delete mode 100644 examples/ajax_upload/ajax_upload.c delete mode 100644 examples/ajax_upload/index.html diff --git a/examples/ajax_upload/Makefile b/examples/ajax_upload/Makefile deleted file mode 100644 index 678f6ba6e..000000000 --- a/examples/ajax_upload/Makefile +++ /dev/null @@ -1,3 +0,0 @@ -PROG = ajax_upload -MODULE_CFLAGS = -DMG_ENABLE_HTTP_STREAMING_MULTIPART -include ../examples.mk diff --git a/examples/ajax_upload/ajax_upload.c b/examples/ajax_upload/ajax_upload.c deleted file mode 100644 index 662aa2d61..000000000 --- a/examples/ajax_upload/ajax_upload.c +++ /dev/null @@ -1,85 +0,0 @@ -// Copyright (c) 2017 Cesanta Software Limited -// All rights reserved - -#include "mongoose.h" - -static const char *s_http_port = "8000"; -static struct mg_serve_http_opts s_http_server_opts; - -static void ev_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); - } -} - -static void upload_handler(struct mg_connection *nc, int ev, void *p) { - (void) nc; - struct http_message *hm = (struct http_message *) p; - if (ev == MG_EV_HTTP_REQUEST) { - char file_name[256]; - FILE *f = NULL; - if (mg_get_http_var(&hm->query_string, "file_name", file_name, - sizeof(file_name)) <= 0) { - mg_printf(nc, "%s", - "HTTP/1.1 500 File name is not specified\r\n" - "Content-Length: 0\r\n\r\n"); - nc->flags |= MG_F_SEND_AND_CLOSE; - goto cleanup; - } - printf("Saving file %s, size %d bytes\n", file_name, (int) hm->body.len); - f = fopen(file_name, "w"); - if (f == NULL) { - mg_printf(nc, "%s", - "HTTP/1.1 500 Failed to open a file\r\n" - "Content-Length: 0\r\n\r\n"); - nc->flags |= MG_F_SEND_AND_CLOSE; - goto cleanup; - } - if (fwrite(hm->body.p, 1, hm->body.len, f) != hm->body.len) { - mg_printf(nc, "%s", - "HTTP/1.1 500 Failed to write to a file\r\n" - "Content-Length: 0\r\n\r\n"); - nc->flags |= MG_F_SEND_AND_CLOSE; - goto cleanup; - } - mg_printf(nc, "%s", - "HTTP/1.1 200 OK\r\n" - "Content-Type: text/html\r\n" - "Connection: close\r\n" - "\r\n" - "File upload successfully"); - nc->flags |= MG_F_SEND_AND_CLOSE; - printf("Done.\n"); - cleanup: - if (f != NULL) { - fclose(f); - } - } -} - -int main(void) { - struct mg_mgr mgr; - struct mg_connection *nc; - - mg_mgr_init(&mgr, NULL); - printf("Starting web server on port %s\n", s_http_port); - nc = mg_bind(&mgr, s_http_port, ev_handler); - if (nc == NULL) { - printf("Failed to create listener\n"); - return 1; - } - - mg_register_http_endpoint(nc, "/upload", upload_handler MG_UD_ARG(NULL)); - - // Set up HTTP server parameters - mg_set_protocol_http_websocket(nc); - s_http_server_opts.document_root = "."; // Serve current directory - s_http_server_opts.enable_directory_listing = "yes"; - - for (;;) { - mg_mgr_poll(&mgr, 1000); - } - mg_mgr_free(&mgr); - - return 0; -} diff --git a/examples/ajax_upload/index.html b/examples/ajax_upload/index.html deleted file mode 100644 index c6e1ccb99..000000000 --- a/examples/ajax_upload/index.html +++ /dev/null @@ -1,47 +0,0 @@ -<!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_name = $('input[type=file]').val().split('\\').pop(); - console.log("uploadFile: ", file_name) - var file_data = document.getElementById('file').files[0]; - $.ajax({ - url: "/upload?file_name=" + file_name, - 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> -- GitLab