C# 文件上传提供';System.NullReferenceException';论邮寄方式

C# 文件上传提供';System.NullReferenceException';论邮寄方式,c#,html,asp.net,file-upload,C#,Html,Asp.net,File Upload,EDIT:问题出在我回发时,jQuery移动库删除了我的文件。我仍然不知道为什么,但我正在尝试制作一个定制的jQuery移动库,看看是什么导致了我的问题。 我试图制作一个表格,人们可以上传他们的简历并发送给我,但我有一些问题。我将表单发送到后端,发送到方法SendJobMail,所有内容都被读取,除了文件上传的内容 我尝试添加断点,但它将在此行中断: string attachmentFilename = Path.GetFileName(CVUpload.PostedFile.FileName

EDIT:问题出在我回发时,jQuery移动库删除了我的文件。我仍然不知道为什么,但我正在尝试制作一个定制的jQuery移动库,看看是什么导致了我的问题。

我试图制作一个表格,人们可以上传他们的简历并发送给我,但我有一些问题。我将表单发送到后端,发送到方法SendJobMail,所有内容都被读取,除了文件上传的内容

我尝试添加断点,但它将在此行中断:

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" />

我想你在使用之前首先需要保存这个文件

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

可能的重复项嗯,我知道错误消息的意思,但我不明白为什么文件上载是空的,而输入的其余部分是内容。您何时调用函数:SendJobMail()?单击submit时是否调用它?它将跳过if语句,因为CVUpload实际上没有文件。我认为文件在回发过程中丢失了……是的,除了文件上传内容以外的所有内容都丢失了。我找到了问题所在,但没有找到解决问题的方法。。我删除了jQuery移动脚本,现在它似乎可以工作了,但是为什么呢?
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();
            }
        }
if (CVUpload.HasFile)
{
    var savePath = @"c:\temp\uploads\" + CVUpload.FileName;
    CVUpload.SaveAs(savePath);
    DisplayFileContents(CVUpload.PostedFile);
}