Webdev upload fail

	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(""))
	system.tag.write('[dhProvider]obj_test_float', basereq.getParts())
	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()))
		system.tag.write('[dhProvider]Obj_0843',part.getSubmittedFileName())
		system.tag.write('[dhProvider]obj_test_float',1)
	return {'html': '<html><body>Hello World</body></html>'}



After uploading the file, basereq Getparts() is an empty array

Oh no haha, what are you doing? The module does the hard work for you, just use this

byteArray = request['data']

I would also use the POST method.

This is the output

Bytearray contains a lot of information. How can you only extract the file information uploaded by yourself

That is the information uploaded. Write those bytes to a file (as bytes) to see for yourself.


How can I extract the data I want, that is, the following

You can not do anything about this, I think a is bug or a limitation.

the WebDev 5.1.7 (b2021060314) module is not ready for multipart/form-data and your desired result.

The work around for this is to include a base64 file in your request. Catch it with base64File = request['params']['yourparamname'] and decode it with Base64 class

ok

I know this is marked as solved, but if other stumble here and want some kind of solution, I parse the POST data as we cannot rely on servletRequest. Only thing is that I prepend date and time to the filename

	from com.inductiveautomation.ignition.gateway import IgnitionGateway
	import os
	import re
    
	if request["servletRequest"].getContentType() and request["servletRequest"].getContentType().startswith("multipart/form-data"):
		today = system.date.now()
		backupTime = system.date.format(today, "yyyy-MM-dd'T'HH_mm_ss")
		dataDir = IgnitionGateway.get().getSystemManager().getDataDir()
		
		postdata = bytearray(request["postData"])
		
		separator = '\n'
		
		if postdata.index('\r\n') < postdata.index(separator):
			separator = '\r\n'
		
		boundary = postdata.split(separator, 1)[0]
		splitGroups = postdata.split(boundary)
		
		index = 0
		length = 0
		lenBoundary = len(boundary)
		lenSeparator = len(separator)
		
		for g in splitGroups:
			sub = g.split(separator * 2, 1)
			index = index + len(sub[0]) + 2 * (lenSeparator if len(sub) > 1 else 0)

			if "filename=\"" in sub[0] and len(sub) > 1:
				p = re.search("filename=\"([^\"]*)\"", sub[0])
				filepath = os.path.join(dataDir.getAbsolutePath(), "somesubfolder", "%s-%s" % (backupTime, p.group(1)))
				system.file.writeFile(filepath, request["postData"][index:index + len(sub[1]) - lenSeparator])
			
			if len(sub) > 1:
				index = index + len(sub[1])
			
			index = index + lenBoundary