Reputation: 361
I am working on a small project where I have an html form on a website and I wanna use google app script (GAS) for get all form input data and send it into an email address.
So far my code works pretty well; However, I have problem to get the file from my on my html page
Here is the code I am using on GAS
function doPost(request) {
var fname = request.parameters.txtName, femail = request.parameters.txtEmail, fnote = request.parameters.txtArea, ffile = request.parameters.fFileAttach;
MailApp.sendEmail({
to: "[email protected]",
subject: "Message from webform by: " + femail,
htmlBody: "This message was sent by the user " + fname + "<br/>" +
"User email address: " + femail + "<br />" +
"Email message:<br/>" + fnote,
replyTo: femail,
attachments: [ffile]
});
}
Here is my html form code:
<!DOCTYPE html>
<html>
<head>
<base target="_top">
</head>
<body>
<form action="myGAS_Link" id="cc-form" method="post">
<div class="fTop">
<span>Name:* </span><input name="txtName" type="text" placeholder="Your Name" id="fname" required="" autofocus>
<span>E-Mail:* </span><input name="txtEmail" type="email" placeholder="Your e-Mail" id="femail" required="">
</div>
<!--------------------------->
<div class="fSet1">
<span>Notes:</span><br/>
<textarea name="txtArea" id="fTextArea" rows="8"></textarea>
</div>
<!--------------------------->
<div class="fSub">
<span>Upload your File:* </span><input name="fFileAttach" id="fFileAttach" type="file" required>
<input id="submit" type="submit">
</div>
</form>
</body>
</html>
Upvotes: 0
Views: 1875
Reputation: 201428
How about this modification?
FileReader()
, retrieve base64 encoded file, filename and mimetype.input
with type="hidden"
to #cc-form
as text data. replace(/^.*,/, '')
is used for removing a header of encoded data. match(/^.*(?=;)/)[0]
is used for retrieving mimetype of uploading file.request.parameters.data
, request.parameters.filename
and request.parameters.mimetype
, respectively.Utilities.base64Decode()
.Utilities.newBlob()
.function doPost(request) {
var data = Utilities.base64Decode(request.parameters.data); // Added
var ffile = Utilities.newBlob(data, request.parameters.mimetype, request.parameters.filename); // Added
var fname = request.parameters.txtName, femail = request.parameters.txtEmail, fnote = request.parameters.txtArea; // Modified
MailApp.sendEmail({
to: "[email protected]",
subject: "Message from webform by: " + femail,
htmlBody: "This message was sent by the user " + fname + "<br/>" +
"User email address: " + femail + "<br />" +
"Email message:<br/>" + fnote,
replyTo: femail,
attachments: [ffile]
});
return ContentService.createTextOutput("ok"); // Added
}
<!DOCTYPE html>
<html>
<head>
<base target="_top">
<script src="https://ajax.googleapis.com/ajax/libs/jquery/1.12.4/jquery.js"></script> <!-- Added -->
</head>
<body>
<form action="myGAS_Link" id="cc-form" method="post">
<div class="fTop">
<span>Name:* </span><input name="txtName" type="text" placeholder="Your Name" id="fname" required="" autofocus>
<span>E-Mail:* </span><input name="txtEmail" type="email" placeholder="Your e-Mail" id="femail" required="">
</div>
<!--------------------------->
<div class="fSet1">
<span>Notes:</span><br/>
<textarea name="txtArea" id="fTextArea" rows="8"></textarea>
</div>
<!--------------------------->
<div id="data"></div> <!-- Added -->
<div class="fSub">
<span>Upload your File:* </span><input name="fFileAttach" id="fFileAttach" type="file" required>
<input id="submit" type="submit">
</div>
</form>
</body>
<!-- Added -->
<script>
$('#fFileAttach').on("change", function() {
var file = this.files[0];
var fr = new FileReader();
fr.fileName = file.name;
fr.onload = function(e) {
html = '<input type="hidden" name="data" value="'+e.target.result.replace(/^.*,/, '')+'" >';
html += '<input type="hidden" name="filename" value="'+e.target.fileName+'" >';
html += '<input type="hidden" name="mimetype" value="' + e.target.result.match(/^.*(?=;)/)[0] + '" >';
$("#data").empty().append(html); // Modified
}
fr.readAsDataURL(file);
});
</script>
</html>
var file = this.files[0];
means the selected first file. When there are some files, it is files[1], files[2] ...
.If I misunderstand your question, I'm sorry.
<div id="data"></div>
$("#cc-form").append(html);
to $("#data").empty().append(html);
By these modifications, the last selected file is uploaded.
Upvotes: 2