0

I am trying to create a solution which emails you the information of a person. The info is easy to handle since it is string but I couldn't figure out how to upload a file and feed to the aspx in order to send the mail with attachment. I didn't include my tries in the codes above because they just keeped crashing the program. Here are my codes:

HTMLPage.html:

<!DOCTYPE html>
<html xmlns="http://www.w3.org/1999/xhtml">
<head>
    <title></title>
</head>
<body>
    <script src="Scripts/jquery-1.12.0.js" type="text/javascript"
            charset="utf-8"></script>
    <script src="Scripts/functions.js" type="text/javascript"
            charset="utf-8"></script>
    <form id="validate-form" method="post">
        <p>First Name: </p>
        <input type="text" id="name" />
        <p>Last Name:</p>
        <input type="text" id="lname" />
        <p>E-mail: </p>
        <input type="text" id="mail" />
        <p>CV: </p>
        <input type="file" id="CV" />
    </form>
    <button id="button" on""></button>
</body>
</html>

functions.js

$(function () {
    $('#button').click(function (e) {
        e.preventDefault();
        var name = document.getElementById("name").value;
        var lastName = document.getElementById("lname").value;
        var mail = document.getElementById("mail").value;
        var data = new FormData();
        $.ajax({
                    type: "POST",
                    url: '/AjaxHandler.aspx/Handle',
                    data: "{name:'" + name + "', lastName:'" + lastName + "', Mail:'" + mail + "'}",
                    contentType: "application/json; charset=utf-8",
                    dataType: "json",
                    success: alert("Gönderildi")

                });

    }
)});

AjaxHandler.aspx.cs

using System;
using System.Collections.Generic;
using System.Linq;
using System.Web;
using System.Web.UI;
using System.Web.UI.WebControls;
using System.Net.Mail;
using System.IO;
using System.Configuration;
using System.Net.Configuration;
using System.Web.Configuration;
using System.Web.Services;

public partial class AjaxHandler : Page
{
    [WebMethod]
    public static void Handle(string name, string lastName,string Mail)
    {
        try
        {
            SmtpSection cfg = NetSectionGroup.GetSectionGroup(WebConfigurationManager.OpenWebConfiguration("~/web.config")).MailSettings.Smtp;
            MailMessage mail = new MailMessage();
            mail.To.Add("mail@address.com");
            mail.Body = ("Name: " + name + " Last Name: " + lastName + " Email Adress: " + Mail);

            SmtpClient smtpClient = new SmtpClient(cfg.Network.Host, 587);
            smtpClient.EnableSsl = true;
            smtpClient.Send(mail);
        }
        catch
        {
        }
    }
}
Emir Arditi
  • 107
  • 9
  • `var data = new FormData($('#validate-form')[0]);`, then give that variable to the `data` property of the object you provide to `$.ajax`. From there you can access the uploaded file in ASP.Net as normal. See this question for more detail: http://stackoverflow.com/questions/166221/how-can-i-upload-files-asynchronously?rq=1 – Rory McCrossan Jan 28 '16 at 15:35
  • Check out this example: http://aspdotnet-alam.com/Article/asynchronous-file-upload-using-ajax-and-jquery-in-Asp-Net-C-Sharp-71.aspx – Ricardo Pontual Jan 28 '16 at 15:43

0 Answers0