Skip to content

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

@hamoid

Description

@hamoid

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

Metadata

Metadata

Assignees

No one assigned

    Labels

    No labels
    No labels

    Projects

    No projects

    Milestone

    No milestone

    Relationships

    None yet

    Development

    No branches or pull requests

    Issue actions