1 2 3 4 5 6 7 8 9 10 11 12 13 14 15 16 17 18 19 20 21 22 23 24 25 26 27 28 29 30 31 32 33 34 35 36 37 38 39 40 41 42 43 44 45 46 47 48 49 50 51 52 53 54 55 56 57 58 59 60 61 62 63 64 65 66 67
|
<html>
<head>
<title>Convert a file to a base64 request</title>
<script type="text/javascript">
function form_submit(e){
console.log(e)
var resultOutput = document.getElementById('resultOutput');
var fileInput = document.getElementById('fileInput');
var fieldInput = document.getElementById('fieldInput');
makeRequestBase64(fileInput.files[0], fieldInput.value, function(err, result){
resultOutput.value = result;
});
return false;
}
function makeRequestBase64(file, fieldName, cb){
var boundary = '\\/\\/\\/\\/\\/\\/\\/\\/\\/\\/\\/\\/\\/\\/\\/\\/\\/\\/\\/\\/\\/\\/\\/\\/\\/\\/\\/\\/\\/';
var crlf = "\r\n";
var reader = new FileReader();
reader.onload = function(e){
var body = '';
body += '--' + boundary + crlf;
body += 'Content-Disposition: form-data; name="' + fieldName + '"; filename="' + escape(file.name)+ '"' + crlf;
body += 'Content-Type: ' + file.type + '' + crlf;
body += 'Content-Transfer-Encoding: base64' + crlf
body += crlf;
body += e.target.result.substring(e.target.result.indexOf(',') + 1) + crlf;
body += '--' + boundary + '--';
var head = '';
head += 'POST /upload HTTP/1.1' + crlf;
head += 'Host: localhost:8080' + crlf;
head += 'Content-Type: multipart/form-data; boundary=' + boundary + '' + crlf;
head += 'Content-Length: ' + body.length + '' + crlf;
cb(null, head + crlf + body);
};
reader.readAsDataURL(file);
}
</script>
</head>
<body>
<form action="" onsubmit="return form_submit();">
<label>File: <input id="fileInput" type="file" /></label><br />
<label>Field: <input id="fieldInput" type="text" value="file" /></label><br />
<button type="submit">Ok!</button><br />
<label>Request: <textarea id="resultOutput" readonly="readonly" rows="20" cols="80"></textarea></label><br />
</form>
<p>
Don't forget to save the output with windows (CRLF) line endings!
</p>
</body>
</html>
|