Skip to content

Commit

Permalink
Pendant upload file
Browse files Browse the repository at this point in the history
  • Loading branch information
HomineLudens committed Jun 15, 2015
1 parent dc7f745 commit 9ac0fe7
Show file tree
Hide file tree
Showing 3 changed files with 84 additions and 3 deletions.
55 changes: 55 additions & 0 deletions CNCPendant.py
Original file line number Diff line number Diff line change
Expand Up @@ -13,6 +13,7 @@
import json
import threading
import urllib
import re

try:
import urlparse
Expand Down Expand Up @@ -101,6 +102,60 @@ def do_GET(self):
else:
self.mainPage(page[1:])

#----------------------------------------------------------------------
def deal_post_data(self):
boundary = self.headers.plisttext.split("=")[1]
remainbytes = int(self.headers['content-length'])
line = self.rfile.readline()
remainbytes -= len(line)
if not boundary in line:
return (False, "Content NOT begin with boundary")
line = self.rfile.readline()
remainbytes -= len(line)
fn = re.findall(r'Content-Disposition.*name="file"; filename="(.*)"', line)
if not fn:
return (False, "Can't find out file name...")
path = os.path.expanduser("~")
path = os.path.join(path, "bCNCUploads")
if not os.path.exists(path):
os.makedirs(path)
fn = os.path.join(path, fn[0])
line = self.rfile.readline()
remainbytes -= len(line)
line = self.rfile.readline()
remainbytes -= len(line)
try:
out = open(fn, 'wb')
except IOError:
return (False, "Can't create file to write, do you have permission to write?")

preline = self.rfile.readline()
remainbytes -= len(preline)
while remainbytes > 0:
line = self.rfile.readline()
remainbytes -= len(line)
if boundary in line:
preline = preline[0:-1]
if preline.endswith('\r'):
preline = preline[0:-1]
out.write(preline)
out.close()
return (True, "%s" % fn)
else:
out.write(preline)
preline = line
return (False, "Unexpected Ends of data.")


#----------------------------------------------------------------------
def do_POST(self):
result,fMsg=self.deal_post_data()
if(result):
httpd.app._pendantFileUploaded=fMsg
#send empty response so browser does not generate errors
self.do_HEAD(200, "text/text")


# ---------------------------------------------------------------------
def mainPage(self, page):
global prgpath
Expand Down
6 changes: 6 additions & 0 deletions bCNC.py
Original file line number Diff line number Diff line change
Expand Up @@ -362,6 +362,7 @@ def __init__(self, master, **kw):
self._wcsUpdate = False
self._probeUpdate= False
self._gUpdate = False
self._pendantFileUploaded = None
self.running = False
self._runLines = 0
#self._runLineMap = []
Expand Down Expand Up @@ -3952,6 +3953,11 @@ def monitorSerial(self):
if self._gcount >= self._runLines:
self.runEnded()

# Load file from pendant
if self._pendantFileUploaded!=None:
self.load(self._pendantFileUploaded)
self._pendantFileUploaded=None

self.after(MONITOR_AFTER, self.monitorSerial)

#----------------------------------------------------------------------
Expand Down
26 changes: 23 additions & 3 deletions index.html
Original file line number Diff line number Diff line change
Expand Up @@ -182,6 +182,20 @@
sendGcode("G90");
} // sendMove

function fileChange()
{
var formData = new FormData($('#upload-file')[0]);
$.ajax({
url: '/upload', //Server script to process data
data: formData,
type: 'POST',
cache: false,
contentType: false,
processData: false
});
}


/* PERFORM THESE ACTIONS ONCE THE PAGE HAS LOADED */
$(document).ready(function () {
//set up fast click to handle mobile browser delay
Expand Down Expand Up @@ -287,10 +301,16 @@ <h1>bCNC Pendant</h1>
<br />

<div class="pure-g">
<div class="pure-u-1">
<div class="pure-u-1-2">
<span style="padding-top:2px;">Command:</span>
<input id="cmd" size="50" style="width:50%"/>
<button onclick="sendCmd(encodeURIComponent($('#cmd').val())); $('#cmd').val('');" class="button">Send</button>
<input id="cmd" style="width:75%"/>
<button onclick="sendCmd(encodeURIComponent($('#cmd').val())); $('#cmd').val('');" class="button">Send</button>
</div>
<div class="pure-u-1-2">
<form id="upload-file" style="width:75%" enctype="multipart/form-data">
Select file to upload:
<input name="file" type="file" onchange="fileChange();" />
</form>
</div>
</div>

Expand Down

0 comments on commit 9ac0fe7

Please sign in to comment.