mongoose上傳文件

1,html中  通過 form-data方式的。 是走的代碼中的   handle_upload,上傳後文件按照原來的名字保存在根目錄。

2,html中 通過ajax方式上傳的 走的是   handle_upload_bk 裏面的部分,但是這個我沒法提取出來文件名。

3,還有就是走 handle_upload_bk  的時候,從 form-data上傳。 顯示也能上傳。 但是不知道走到哪裏的邏輯了。 走的handle_upload_bk 這個邏輯。

補充:在  handle_upload_bk  裏面   mp->file_name就可以獲取 文件名了。。 終於找到了

<!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>
  <h1>Upload file using standard form upload</h1>

  <form method="POST" action="/upload" enctype="multipart/form-data">
    <label>Select a file:</label><br>
    <input type="file" name="file" />
    <input type="submit" value="Upload" />
  </form>

  <h1>Upload file using Ajax - that also gives progress report</h1>
  <form method="post" id="filename" name="filename" οnsubmit="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>

 

 

// Copyright (c) 2015 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;


struct file_writer_data {
	FILE *fp;
	size_t bytes_written;
};

static void handle_upload_bk(struct mg_connection *nc, int ev, void *p) {
	struct file_writer_data *data = (struct file_writer_data *) nc->user_data;
	struct mg_http_multipart_part *mp = (struct mg_http_multipart_part *) p;

	switch (ev) {
	case MG_EV_HTTP_PART_BEGIN: {
		if (data == NULL) {
			data = calloc(1, sizeof(struct file_writer_data));
			data->fp = tmpfile();
			data->bytes_written = 0;

			if (data->fp == 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;
				free(data);
				return;
			}
			nc->user_data = (void *)data;
		}
		break;
	}
	case MG_EV_HTTP_PART_DATA: {
		if (fwrite(mp->data.p, 1, mp->data.len, data->fp) != mp->data.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;
			return;
		}
		data->bytes_written += mp->data.len;
		break;
	}
	case MG_EV_HTTP_PART_END: {
		mg_printf(nc,
			"HTTP/1.1 200 OK\r\n"
			"Content-Type: text/plain\r\n"
			"Connection: close\r\n\r\n"
			"Written %ld of POST data to a temp file\n\n",
			(long)ftell(data->fp));
		nc->flags |= MG_F_SEND_AND_CLOSE;
		fclose(data->fp);
		free(data);
		nc->user_data = NULL;
		break;
	}
	}
}


struct mg_str cb(struct mg_connection *c, struct mg_str file_name) {
	// Return the same filename. Do not actually do this except in test!
	// fname is user-controlled and needs to be sanitized.
	return file_name;
}

void  handle_upload(struct mg_connection *c, int ev, void *ev_data) {
	switch (ev) {
	case MG_EV_HTTP_PART_BEGIN:
	case MG_EV_HTTP_PART_DATA:
	case MG_EV_HTTP_PART_END:
	case MG_EV_HTTP_MULTIPART_REQUEST_END:
		mg_file_upload_handler(c, ev, ev_data, cb);
		break;
	}
}

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);
  }
}

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;
  }

  // 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";

  mg_register_http_endpoint(nc, "/upload", handle_upload MG_UD_ARG(NULL));

  for (;;) {
    mg_mgr_poll(&mgr, 1000);
  }
  mg_mgr_free(&mgr);

  return 0;
}

 

發表評論
所有評論
還沒有人評論,想成為第一個評論的人麼? 請在上方評論欄輸入並且點擊發布.
相關文章