FileUpload在发布到方法时给出'System.NullReferenceException'

本文关键字:NullReferenceException System 方法 FileUpload | 更新日期: 2023-09-27 18:32:32

编辑:问题恰好是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: 

我也尝试更改文件上传,但这样做,但这也不起作用:

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

FileUpload在发布到方法时给出'System.NullReferenceException'

我认为您首先需要保存文件才能使用它。

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

详细信息: 文件上载 - MSDN