I have thttpd server on embedded device. I want to make firmware update, so i need to send binary files via Ethernet to thttpd server.
Server have recieving file, BUT when i check file in server folder it have smaller size and seems like all 0x00 in file has beed removed. I think that somewhere in the course of this HTTP transaction, the data from the file is interpreted as text and 0x00 is interpreted as an empty field, but I do not know where, how to fix this.
I try to upload file on server with this code:
<form method="post" enctype="multipart/form-data">
<input id="jic-file" type="file" onchange="upload_firmware(this)">
</form>
<script>
async function upload_firmware(inp)
{
let formData = new FormData();
let fw = inp.files[0];
formData.append("firmware", fw);
const ctrl = new AbortController()
setTimeout(() => ctrl.abort(), 10000);
try {
let r = await fetch("new_jic.cgi",
{method: "POST", body: formData, signal: ctrl.signal});
console.log('HTTP response code:',r.status);
} catch(e) {
console.log('Problem with uploading firmware...:', e);
}
}
</script>
And recieve file with new_jic.cgi file:
#!/bin/sh
if [ "$REQUEST_METHOD" = "POST" ]; then
if [ "$CONTENT_LENGTH" -gt 0 ]; then
in_raw=$(cat)
echo $in_raw > raw.jic;
boundary=$(echo -n "$in_raw" | head -1 | tr -d '\r\n');
filename=$(echo -n "$in_raw" | grep --text --max-count=1 -oP "(?<=filename=\")[^\"]*");
file_content=$(echo -n "$in_raw" | sed '1,/Content-Type:/d' | tail -c +3 | head --lines=-1 | head --bytes=-2);
#echo "boundary: $boundary"
#echo "filename: $filename"
echo $file_content > $filename;
fi
fi
exit 0
Also try this code:
<script>
async function upload_firmware(inp)
{
var fw = document.querySelector('input[type=file]').files[0];
var bloob = new Blob([fw]);
let r = await fetch("new_jic.cgi",
{method: "POST", body: bloob});
console.log('HTTP response code:',r.status);
}
</script>