EDIT: The problem happened to be the jQuery-Mobile library that removed my file when i was doing a postback. I still have no idea why, but I'm trying to make a custom jQuery-Mobile library now to see what causes my problem.
I'm trying to make a form where people can upload their resumé and send it to me, but I'm having some issues with it. I'm sending the form to the backend, to the method SendJobMail and everything gets read out, except for the content from the FileUpload.
I tried adding breakpoints and it'll break at this line:
string attachmentFilename = Path.GetFileName(CVUpload.PostedFile.FileName);
It gives me the following error message:
System.NullReferenceException was unhandled by user code
HResult=-2147467261
Message=Object reference not set to an instance of an object.
Source=PublicSite_ASP
StackTrace:
at PublicSite_ASP.TestPage.SendJobMail() in c:\Users\joris.decraecker\Documents\Visual Studio 2013\Projects\Public Website-Mobile\PublicSite_ASP\default.aspx.cs:line 225
at PublicSite_ASP.TestPage.Page_Load(Object sender, EventArgs e) in c:\Users\joris.decraecker\Documents\Visual Studio 2013\Projects\Public Website-Mobile\PublicSite_ASP\default.aspx.cs:line 31
at System.Web.Util.CalliEventHandlerDelegateProxy.Callback(Object sender, EventArgs e)
at System.Web.UI.Control.OnLoad(EventArgs e)
at System.Web.UI.Control.LoadRecursive()
at System.Web.UI.Page.ProcessRequestMain(Boolean includeStagesBeforeAsyncPoint, Boolean includeStagesAfterAsyncPoint)
InnerException:
I also tried changing the FileUpload but to this, but that didn't work either:
<label>Selecteer uw CV</label>
<asp:FileUpload runat="server" ID="CVUpload" />
.
Here is my current code:
HTML:
<form runat="server" id="frmApplication" name="frmApplication" role="form" action="default.aspx/SendJobMail" enctype="multipart/form-data">
<div class="row clearfix">
<asp:HiddenField runat="server" ID="vacancyId" Value="" />
<div class="col-sm-6 column">
<div class="row clearfix">
<div class="col-md-12 column">
<div class="row clearfix">
<div class="col-sm-6 column form-column">
<label for="txtNameJob" class="sr-only">Voornaam</label>
<input runat="server" id="txtNameJob" name="firstName" type="text" class="form-control input-lg" placeholder="Voornaam" />
</div>
<div class="col-sm-6 column form-column">
<label for="txtSurnameJob" class="sr-only">Achternaam</label>
<input runat="server" id="txtSurnameJob" name="lastName" class="form-control input-lg" type="text" placeholder="Achternaam" />
</div>
</div>
<div class="row clearfix">
<div class="col-md-12 column form-column">
<label for="txtEmailJob" class="sr-only">Email</label>
<input runat="server" id="txtEmailJob" name="email" class="form-control input-lg" type="text" placeholder="Email" />
</div>
</div>
<div class="row clearfix">
<div class="col-md-12 column form-column">
<label for="txtTel" class="sr-only">Telefoon</label>
<input runat="server" id="txtTel" name="phone" class="form-control input-lg" type="text" placeholder="Telefoonnummer" />
</div>
</div>
</div>
</div>
</div>
<div class="col-sm-6 column">
<label for="txtMessageJob" class="sr-only">Motivatie</label>
<textarea runat="server" id="txtMessageJob" name="motivation" class="form-control input-lg" rows="7" placeholder="Motivatie"></textarea>
</div>
</div>
<div class="row clearfix">
<div class="col-md-12 column">
<div class="row clearfix">
<div class="col-sm-6 column">
<div class="input-group form-column">
<span class="input-group-btn">
<span class="btn btn-primary btn-lg btn-file">
Bladeren...
<asp:FileUpload runat="server" ID="CVUpload" />
</span>
</span>
<input runat="server" type="text" id="fileTextField" class="form-control input-lg" placeholder="Selecteer uw CV" readonly="true" />
</div>
</div>
<div class="col-sm-6 column">
<input id="btnSubmit" type="submit" value="Solliciteren" class="btn btn-primary btn-lg pull-right" />
</div>
</div>
</div>
</div>
</form>
Codebehind:
private void SendJobMail()
{
var client = new MailerServiceReference.IemailClient();
bool succeeded = false;
string recipient = "jobs@domain.eu";
string firstName = txtNameJob.Value;
string lastName = txtSurnameJob.Value;
string telNr = txtTel.Value;
string motivation = txtMessageJob.Value;
string sender = "info@domain.eu";
int vacId = Int32.Parse(vacancyId.Value);
string vacTitle = _vacancies.Find(vac => vac.Id == vacId).Title;
string subject = string.Format("Application from {0} {1} for {2}", firstName, lastName, vacTitle);
motivation += string.Format("\n\n{0}{1}\n{2}\n{3}", firstName, lastName, txtEmailJob.Value, telNr);
//set up and add an attachment
string attachmentFilename = Path.GetFileName(CVUpload.PostedFile.FileName);
Attachment attachment = new Attachment(CVUpload.FileContent, attachmentFilename, MediaTypeNames.Application.Octet); //can interpret PDF and richtextdocument
ContentDisposition disposition = attachment.ContentDisposition; //disposition necessary for some mailing applications
disposition.CreationDate = File.GetCreationTime(attachmentFilename);
disposition.ModificationDate = File.GetLastWriteTime(attachmentFilename);
disposition.ReadDate = File.GetLastAccessTime(attachmentFilename);
disposition.FileName = Path.GetFileName(attachmentFilename);
disposition.Size = CVUpload.PostedFile.ContentLength;
disposition.DispositionType = DispositionTypeNames.Attachment;
try
{
client.Open();
succeeded = client.SendEmail("PublicSite", sender, "info@domain.eu", subject, motivation);
}
catch (Exception e)
{
Console.WriteLine("lalala " + e.Message);
}
finally
{
client.Close();
}
}