I have a form but the user's data is not stored anywhere, just sent in an email. Does anyone know of an easy way to allow the user to attach a file?
As of now, when the user clicks submit, jquery collects the data and sends it to 'AjaxController.cs'. (A form element is not used)
HTML
<div class="form">
<input type="text" name="Name">
<input type="file" name="File">
<button>Submit</button>
</div>
JS
$(document).ready(function(){
$('button').click(function(){
var data = {};
$('input').each(function(){
data[this.name] = this.value;
}
$.post('/Ajax/Email', data, function(){
alert("Email Sent");
});
});
}
C#
public class AjaxController : Controller
{
public ActionResult Email()
{
MailMessage message = new MailMessage("from@fake.com","marketing@fake.com");
foreach (string form_inputs in Request.Form.Keys)
{
String input_name = form_inputs.ToString();
String input_value = Request.Form[form_inputs].Trim();
if(input_name == "File")
{
System.Net.Mail.Attachment attachment;
attachment = new System.Net.Mail.Attachment(input_value); //ERROR
message.Attachments.Add(attachment);
}
if (input_name == "Name")
{
message.Body = "Name: " + input_value;
}
}
SmtpClient client = new SmtpClient();
client.Port = 25;
client.DeliveryMethod = System.Net.Mail.SmtpDeliveryMethod.Network;
client.UseDefaultCredentials = false;
client.Host = "SMTP.fake.com";
client.Send(message);
}
}
/Ajax/Email simply returns 'Mail Sent!"
I'm getting an error that basically says the uploaded file does not exist locally - of course - because it hasn't been uploaded. Where does the file exist at this point, how do I access it?