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

Implement or show how to upload files to your sketch #7

Open
hamoid opened this issue Nov 11, 2019 · 1 comment
Open

Implement or show how to upload files to your sketch #7

hamoid opened this issue Nov 11, 2019 · 1 comment

Comments

@hamoid
Copy link

hamoid commented Nov 11, 2019

Hi! :) I was wondering if it's possible to upload files to the server?

I tried this:

import http.*;
import java.util.Map;

SimpleHTTPServer server;

void setup() {
  SimpleHTTPServer.useIndexHtml = false;
  server = new SimpleHTTPServer(this); 
  TemplateFileHandler templateHandler = new ResultFromPost("index.ftl");
  server.createContext("", templateHandler);
}

class ResultFromPost extends TemplateFileHandler {
  public ResultFromPost(String templateFileName) {
    super(templateFileName);
  }
  void createMap() {
    Map<String, String> params = queryToMap();
    println(params.get("image"));
  }
}

And this is the ftl

<!DOCTYPE html>
<html>
	<header></header>
	<body>
	<form  action="" method="post" enctype="multipart/form-data">
		<input type="file" name="image">
		<input type="submit" value="Upload" />
	</form>
	</body>
</html>

The data is there printed in the console, so that part works, but the data must be decoded. Does SimpleHTTPServer come with any helper functions for this?

A nice Example for this feature would be a tiny image gallery. Allow uploading images, and then show them as thumbnails in the html. Maybe convert them to black and white in Processing before saving them :)

Here some info about the decoding part:

https://stackoverflow.com/questions/3337056/convenient-way-to-parse-incoming-multipart-form-data-parameters-in-a-servlet

@transfluxus
Copy link
Owner

Hi,

I think there would be some things todo in the DynamicResponseHandler, which tells it that the method is post with content-type: multipart/form-data. The Handler (SHTTPSHandler) uses com.sun.net.httpserver.HttpExchange, for which I couldn't find anything that does that.
only the head can be derived like this, which could be the beginning:

DynamicResponseHandler responder1;

void setup() {
  SimpleHTTPServer.useIndexHtml = false;
  server = new SimpleHTTPServer(this); 
  TemplateFileHandler templateHandler = new ResultFromPost("index.ftl");
  server.createContext("", templateHandler);
  
  responder1 = new DynamicResponseHandler(new FileUpload(), "application/json");
  server.createContext("upload", responder1); 
}

class FileUpload extends ResponseBuilder {
    public  String getResponse(String requestBody) {
      //println(requestBody.split("\n")[0]);
      println(parent.getExchange().getRequestHeaders().values());
      return "";
    }
}

see: http://www.docjar.com/html/api/sun/net/httpserver/UnmodifiableHeaders.java.html

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

2 participants