#include "webcit.h"
+
// This function is called by the MIME parser to handle data uploaded by the browser.
void upload_handler(char *name, char *filename, char *partnum, char *disp,
void *content, char *cbtype, char *cbcharset,
}
write(fd, content, length);
close(fd);
+
+ JsonValue *j_uploads = (JsonValue *) userdata;
+ JsonArrayAppend(j_uploads, NewJsonNumber(HKEY("foo"), 1));
+ JsonArrayAppend(j_uploads, NewJsonNumber(HKEY("bar"), 2));
+ JsonArrayAppend(j_uploads, NewJsonNumber(HKEY("baz"), 3));
}
// upload handler
void upload_files(struct http_transaction *h, struct ctdlsession *c) {
// FIXME reject uploads if we're not logged in
+ // This will be a JSON Array of all files that were uploaded during this HTTP transaction.
+ // Normally the browser will upload only one file per transaction, but that behavior is not guaranteed.
+ JsonValue *j_uploads = NewJsonArray(HKEY(""));
+
// h->request_body will contain the upload(s) in MIME format
- mime_parser(h->request_body, (h->request_body + h->request_body_length), *upload_handler, NULL, NULL, NULL, 0);
+ mime_parser(h->request_body, (h->request_body + h->request_body_length), *upload_handler, NULL, NULL, j_uploads, 0);
// probably do something more clever here
h->response_code = 200;
h->response_string = strdup("OK");
+
+ // send back a JSON array of all files uploaded
+ StrBuf *sj = NewStrBuf();
+ SerializeJson(sj, j_uploads, 1); // '1' == free the source object
add_response_header(h, strdup("Content-type"), strdup("application/json"));
- h->response_body = "{}";
- h->response_body_length = strlen(h->response_body);
+ h->response_code = 200;
+ h->response_string = strdup("OK");
+ h->response_body_length = StrLength(sj);
+ h->response_body = SmashStrBuf(&sj);
}
\ No newline at end of file
xhr.open('POST', url, true);
xhr.addEventListener('readystatechange', function(e) {
+ console.log("readyState: " + xhr.readyState);
if (xhr.readyState == 4 && xhr.status == 200) {
- //document.getElementById("ctdl-upload_list").innerHTML += "<li>succeeeeed</li>";
num_attachments += 1;
document.getElementById("num_attachments").innerHTML = num_attachments;
let li = document.getElementById("ctdl_uploading_" + uploads_in_progress.toString());
li.parentNode.removeChild(li);
uploads_in_progress -= 1;
+
+ // what happened?
+ console.log("response: " + xhr.response);
+ console.log("responseText: " + xhr.responseText);
}
else if (xhr.readyState == 4 && xhr.status != 200) {
- //document.getElementById("ctdl-upload_list").innerHTML += "<li>EPIC FAIL</li>";
-
- // remove the "uploading in progress" message (maybe we should replace it with an error?)
+ // remove the "uploading in progress" message (there was an error, so just let it disappear)
let li = document.getElementById("ctdl_uploading_" + uploads_in_progress.toString());
li.parentNode.removeChild(li);
uploads_in_progress -= 1;
}
-
// Show or hide the attachments window in the composer
function show_or_hide_attachments() {