Hardware Reference
In-Depth Information
8
Now you need a method to
formulate and send the actual
POST request. To do this, open a new
Client connection to the server, then
send the request. In this method, the
content of the request is broken into
three parts: everything that comes
before the file itself; the actual file,
which is loaded into a byte array; and
the end of the request that comes after
the file. Add this method at the end of
your sketch.
void postPicture() {
// load the saved image into an array of bytes:
byte[] thisFile =loadBytes(fileName);
// open a new connection to the server:
thisClient = new Client(this, "www.example.com", 80);
// make an HTTP POST request:
thisClient.write("POST " + pictureScriptUrl + " HTTP/1.1\n");
thisClient.write("Host: www.example.com\n");
// tell the server you're sending the POST in multiple parts,
// and send a unique string that will delineate the parts:
thisClient.write("Content-Type: multipart/form-data; boundary=");
thisClient.write(boundary + "\n");
// form the beginning of the request:
String requestHead ="\n--" + boundary + "\n";
requestHead +="Content-Disposition: form-data; name=\"file\"; ";
requestHead += "filename=\"" + fileName + "\"\n";
requestHead +="Content-Type: image/jpeg\n\n";
// form the end of the request:
String tail ="\n--" + boundary + "--\n\n";
8
Once you've got the three pieces of
the request, calculate the total number
of bytes by adding the length of the two
strings and the byte array. Next, send
the content length, and then the content
itself. Finally, you close the connection
by stopping the client.
// calculate and send the length of the total request,
// including the head of the request, the file, and the tail:
int contentLength = requestHead.length() + thisFile.length + tail.length();
thisClient.write("Content-Length: " + contentLength + "\n\n");
// send the header of the request, the file, and the tail:
thisClient.write(requestHead);
thisClient.write(thisFile);
thisClient.write(tail);
// close the client:
thisClient.stop();
}
8
Finally, add a call to this new
method in the keyReleased() method
(new lines are shown in blue).
void keyReleased() {
PImage img = get();
img.save(fileName);
postPicture();
}
Save the sketch and run it. Now when you type any key, the sketch will not only save the image locally, but also upload it
to the catcam directory on your sever, via the save2web.php script.
 
Search WWH ::




Custom Search