Skip to content
New issue

Have a question about this project? Sign up for a free GitHub account to open an issue and contact its maintainers and the community.

By clicking “Sign up for GitHub”, you agree to our terms of service and privacy statement. We’ll occasionally send you account related emails.

Already on GitHub? Sign in to your account

POST request multipart/form-data #193

Closed
7make opened this issue Mar 18, 2018 · 1 comment
Closed

POST request multipart/form-data #193

7make opened this issue Mar 18, 2018 · 1 comment
Assignees

Comments

@7make
Copy link

7make commented Mar 18, 2018

Have any example to work multipart/form-data from request?

[RestRoute(HttpMethod = HttpMethod.POST, PathInfo = "/in.php")]
public IHttpContext Input(IHttpContext context)
{
	if (!context.Request.HasEntityBody)
	{
		context.Response.SendResponse(Grapevine.Shared.HttpStatusCode.InternalServerError, "sorry");
		return context;
	}
	//parse data from request
}

POST request
image

@Yangxing1995
Copy link

Yangxing1995 commented Nov 22, 2018

Use Http-Multipart-Data-Parser.

            int ChunkSize = 1024;
            try
            {
                using (Stream input = ((HttpRequest)context.Request).Advanced.InputStream)
                {
                    var parser = new HttpMultipartParser.MultipartFormDataParser(input);
                   var task= parser.GetParameterValue("Task");
                   var fullAnswer= parser.GetParameterValue("FullAnswer")

                    // Loop through all the files
                    foreach (HttpMultipartParser.FilePart file in parser.Files)
                    {
                        using (BinaryReader reader = new BinaryReader(file.Data, context.Request.ContentEncoding))
                        {
                            using (BinaryWriter output = new BinaryWriter(File.Open(file.FileName, FileMode.Create)))
                            {
                                byte[] chunk = reader.ReadBytes(ChunkSize);
                                while (chunk.Length > 0)
                                {
                                    output.Write(chunk);
                                    chunk = reader.ReadBytes(ChunkSize);
                                }
                            }
                        }
                    }
                }
            }
            catch (Exception e)
            {
            }

Sign up for free to join this conversation on GitHub. Already have an account? Sign in to comment
Labels
None yet
Projects
None yet
Development

No branches or pull requests

3 participants