Webdev image upload?

Hi all,

Has anyone tried to use webdev to receive a file and store it into a database? Just curious as to how this group would go about it.

Thanks,

Oscar.

I would start from the simplest possible case: an HTML form accepting a ‘File’ input, which would then pass the file data (probably either b64 encoded or as a bytestream) to a different endpoint.

Thanks @PGriffith!

The posting side is sorted out; the data is actually coming from a “Google Glass”-type device that will complete an action in an Augmented Reality environment, and post the results of the action (which includes an image) to the Webdev endpoint.

My question was more about how to process that file stream coming into Webdev using Scripting. If anyone has any examples that would be great.

Thanks.
Oscar.

My test setup was this, on a single /upload endpoint.
doGet:

	html = """<html><head></head></body>
	<form method="POST" enctype="multipart/form-data">
		<input type="file" name="filename"><br>
		<input type="submit" value="Submit">
	</form>"""
	return {'html': html}

doPost:

	logger = system.util.getLogger("Webdev Debug")

	from org.eclipse.jetty.server import Request
	from javax.servlet import MultipartConfigElement
	from org.apache.commons.io import IOUtils
	
	basereq = request['servletRequest']
	basereq.setAttribute(Request.__MULTIPART_CONFIG_ELEMENT, MultipartConfigElement(""))
	for part in basereq.getParts():
		logger.info("%s - %s" % (part.getName(), part.getSize()))
		logger.info(str(part.getSubmittedFileName()))
		system.file.writeFile(part.getSubmittedFileName(), IOUtils.toByteArray(part.getInputStream()))

	return {'html': '<html><body>Hello World</body></html>'}
1 Like

I implemented the code above but receive the following:

## HTTP ERROR 500 Traceback (most recent call last): File "<GE_Time_Tracker/python:doPost>", line 11, in doPost AttributeError: type object 'org.eclipse.jetty.server.Request' has no attribute '__MULTIPART_CONFIG_ELEMENT'

|URI:|/system/webdev/GE_Time_Tracker/python|
| --- | --- |
|STATUS:|500|
|MESSAGE:|Traceback (most recent call last): File "<GE_Time_Tracker/python:doPost>", line 11, in doPost AttributeError: type object 'org.eclipse.jetty.server.Request' has no attribute '__MULTIPART_CONFIG_ELEMENT'|
|SERVLET:|Map|

I cannot seem to figure out where i am going wrong with implementing this

Looks like the code sample is old enough that constant was changed. At least in 8.1.2’s version of Jetty, which hasn’t changed in a while, it’s just MULTIPART_CONFIG_ELEMENT - no leading underscores.

Although you may end up running into this issue:

1 Like

Thanks. That worked.