FileUpload给出了System.NullReferenceException'在post to方法上

时间:2014-12-01 09:48:55

标签: c# html asp.net file-upload

编辑:问题恰巧是jQuery-Mobile库在我进行回发时删除了我的文件。我仍然不知道为什么,但我现在正在尝试制作一个自定义的jQuery-Mobile库来查看导致我问题的原因。

我正在尝试制作一个表格,人们可以上传他们的简历并发送给我,但我遇到了一些问题。我将表单发送到后端,方法 SendJobMail ,除了来自 FileUpload 的内容外,所有内容都会被读出。

我尝试添加断点,它会在这一行中突破:

string attachmentFilename = Path.GetFileName(CVUpload.PostedFile.FileName);

它给我以下错误消息:

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: 

我也尝试过改变FileUpload,但是这个也没有用:

<label>Selecteer uw CV</label>
<asp:FileUpload runat="server" ID="CVUpload" />

这是我目前的代码:

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>

代码隐藏:

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();
            }
        }

1 个答案:

答案 0 :(得分:0)

我认为您首先需要在使用之前保存文件..

if (CVUpload.HasFile)
{
    var savePath = @"c:\temp\uploads\" + CVUpload.FileName;
    CVUpload.SaveAs(savePath);
    DisplayFileContents(CVUpload.PostedFile);
}

More Info: FileUpload - MSDN