Comments (20)
UPDATE:
I'm trying to implement it, but when I write code like this in Request.cpp
. The mg_upload
will always return 0.
Any suggestion?
mg_upload(request.connection, "/tmp");
from mongoose-cpp.
Hi
Is the form you are using to post the file good? (It has to be multipart)
from mongoose-cpp.
Thanks for your response.
Yes, I use multipart to send it.
Here is the command to sent file:
curl --form "[email protected]" http://127.0.0.1:8080
It can do the job when I use mongoose.c directly.
Here is my code:
Request.cpp:
...
int Request::upload(string destination_dir)
{
return mg_upload(connection, destination_dir.c_str());
}
Request.h
...
int upload(string destination_dir);
...
upload.cpp
#include <unistd.h>
#include <stdlib.h>
#include <signal.h>
#include <mongoose/Server.h>
#include <mongoose/WebController.h>
using namespace std;
using namespace Mongoose;
class MyController : public WebController
{
public:
void upload(Request &request, StreamResponse &response)
{
response << "Upload test begin " << endl;
response << "Number of file uploaded:" << request.upload(".") << endl;
}
void setup()
{
addRoute("POST", "/", MyController, upload);
}
};
int main()
{
MyController myController;
Server server(8080);
server.registerController(&myController);
server.start();
while(1)
sleep(10);
}
Here is the output:
Upload test begin
Number of file uploaded:0
Any idea?
from mongoose-cpp.
Ok
I guess this is because I systematically read all the post data in the request, so mg_upload
is not fed anymore by mg_read
we'll have to change this behaviour to support post files
from mongoose-cpp.
Line 22 of Request.cpp
should also test if the request is multipart for e.g, and avoid reading post in this case
(I cant work ok this since I am not near a computer now)
from mongoose-cpp.
Yes, you are right.
I just tested PUT method, and it's working!!
I think we need get a universal way to handle data send by client, because not only POST method can send file.
from mongoose-cpp.
Why not, but if we test the Content-type
request header to check for a boundary like mg_upload
we could detect multipart requests
from mongoose-cpp.
Since there are two ways to send data:
application/x-www-form-urlencoded
and multipart/form-data
This may be better:
Request.cpp
...
// Downloading send data
ostringstream sendData;
// if (method == "POST") {
const char * content_type = mg_get_header(connection, "Content-Type");
if (content_type!= NULL && strcmp(content_type, "application/x-www-form-urlencoded") == 0){
int
char post[
while (n = mg_read(connection, post, sizeof(post))) {
sendData.write(post, n);
}
}
data = sendData.str();
...
Also I noticed that you request_info->http_headers array manually to get header, but mongoose.c already offered a convenient function: mg_get_header
.
from mongoose-cpp.
Hi, I have implemented the upload feature, including upload callback(same as you did for begin_request
).
I will create a pull request later.
from mongoose-cpp.
I still don't imagine what is the best way to put the file upload in the binding,
I dont think the callback is really important, we could indeed have a method to save the uploaded file somewhere
from mongoose-cpp.
the mg_upload can decide the location. But If you don't have that call
back, you can't get the file name.
I still don't imagine what is the best way to put the file upload in the
binding,
I dont think the callback is really important, we could indeed have a
method to save the uploaded file somewhere
—
Reply to this email directly or view it on
GitHubhttps://github.com//issues/1#issuecomment-22437831
.
from mongoose-cpp.
You're right
However, I'm not really happy with the way mongoose handle the naming of
the uploaded files, it look like it's always using the client filename
Allowing the conttoller developper to choose the name would be better,
maybe we could implement a method to rename the file to an arbitrary
location
Le 10 août 2013 13:54, "happlebao" [email protected] a écrit :
the mg_upload can decide the location. But If you don't have that call
back, you can't get the file name.I still don't imagine what is the best way to put the file upload in the
binding,I dont think the callback is really important, we could indeed have a
method to save the uploaded file somewhere—
Reply to this email directly or view it on
GitHub<
https://github.com/Gregwar/mongoose-cpp/issues/1#issuecomment-22437831>
.—
Reply to this email directly or view it on GitHubhttps://github.com//issues/1#issuecomment-22438506
.
from mongoose-cpp.
If we want to rename the file, I think it's hard to handle multiple files in one request.
And since the aim of this project is offer a c++ wrapper of mongoose, we need the same functionality as mongoose.
Mongoose has callback, then we need callback too.
from mongoose-cpp.
Yes, we will have to use the callback
Maybe we could create a method that call mg_upload
, then the callback
will give us the name of files and we could access it as a
vector where UploadedFile is a class encapsulating the name
and some methods to rename it, remove it etc.
Le 10 août 2013 14:21, "happlebao" [email protected] a écrit :
If we want to rename the file, I think it's hard to handle multiple files
in one request.
And since the aim of this project is offer a c++ wrapper of mongoose, we
need the same functionality as mongoose.
Mongoose has callback, then we need callback too.—
Reply to this email directly or view it on GitHubhttps://github.com//issues/1#issuecomment-22438876
.
from mongoose-cpp.
But if we do that, then this project is no longer a just simple binding of mongoose.
from mongoose-cpp.
It's already more than that (think of sessions, controllers and routing for
instance)
The goal is not just to wrap mongoose but to provide a developer-friendly
API for writing applications with a webserver embedded
Mongoose is tiny and compiles very fast, which makes it cool but using it
in pure-C style tastes like dealing with buffers of char[]
, sprintf
and
other things that I'm not happy to use in a C++ app :-)
We are not just providing a simple binding but also some additional helpers
to make easier the building of an embedded webapp
Le 10 août 2013 16:46, "happlebao" [email protected] a écrit :
But if we do that, then this project is no longer a just simple binding of
mongoose.—
Reply to this email directly or view it on GitHubhttps://github.com//issues/1#issuecomment-22440999
.
from mongoose-cpp.
Maybe I should change this repo description to something like "C++ web-application library based on mongoose"
from mongoose-cpp.
I've just commited a simple upload system
I think we should now add possibilities for do things with uploaded files like move/rename them
from mongoose-cpp.
(see /upload
in the main.cpp
example)
from mongoose-cpp.
Thanks a lot.
from mongoose-cpp.
Related Issues (20)
- Sessions and multiple controllers does not work HOT 3
- stop() causes segv HOT 1
- Overriding header, doesn't override HOT 2
- SSL usage HOT 1
- Memory leak HOT 2
- State of the project HOT 3
- Can't export dll for mongoose HOT 1
- Changing the IP adress used
- send websocket with large data size HOT 12
- Connecting using http to ssl port causes mongoose to infinite and CPU usage high HOT 2
- How to properly disable MG_DISABLE_SOCKETPAIR in older builds
- How to build on Visual Studio 2015+ HOT 3
- HTTPS protocol error:
- big file upload failed? HOT 1
- Error 1006, JS gets disconnected when CPP sends data
- Wireshark flooded with [PSH, ACK] packets when using websockets HOT 7
- Handling POST requests with Content-Type application/json HOT 4
- compile with warnings - redundant semicolon after namespace closing bracket HOT 2
- Feature request: Support regex matching when matching a url with boost HOT 1
- Bug: regular expression matching doesn't work HOT 1
Recommend Projects
-
React
A declarative, efficient, and flexible JavaScript library for building user interfaces.
-
Vue.js
🖖 Vue.js is a progressive, incrementally-adoptable JavaScript framework for building UI on the web.
-
Typescript
TypeScript is a superset of JavaScript that compiles to clean JavaScript output.
-
TensorFlow
An Open Source Machine Learning Framework for Everyone
-
Django
The Web framework for perfectionists with deadlines.
-
Laravel
A PHP framework for web artisans
-
D3
Bring data to life with SVG, Canvas and HTML. 📊📈🎉
-
Recommend Topics
-
javascript
JavaScript (JS) is a lightweight interpreted programming language with first-class functions.
-
web
Some thing interesting about web. New door for the world.
-
server
A server is a program made to process requests and deliver data to clients.
-
Machine learning
Machine learning is a way of modeling and interpreting data that allows a piece of software to respond intelligently.
-
Visualization
Some thing interesting about visualization, use data art
-
Game
Some thing interesting about game, make everyone happy.
Recommend Org
-
Facebook
We are working to build community through open source technology. NB: members must have two-factor auth.
-
Microsoft
Open source projects and samples from Microsoft.
-
Google
Google ❤️ Open Source for everyone.
-
Alibaba
Alibaba Open Source for everyone
-
D3
Data-Driven Documents codes.
-
Tencent
China tencent open source team.
from mongoose-cpp.