微信公众号搜"智元新知"关注
微信扫一扫可直接关注哦!

一次使用C#MVC后,将HttpPostedFileBase文件内容删除

如何解决一次使用C#MVC后,将HttpPostedFileBase文件内容删除

我正在将表单数据发布到带有一些值和一个Excel文件的控制器中。

然后我要做两件事:1.将文件发送给管理员2.遍历行并保存数据。

我遇到的问题是在发送文件后,文件内容删除并损坏了。

当我尝试遍历行并首先保存数据时,电子邮件中的附件已损坏。

家庭控制器:

[HttpPost]
public ActionResult Index(ImportExcelviewmodel model)
{                       
    try
    {
        var user = (User)Session[Utils.USER_SESSION];

        if (model.File != null)                
            MailClient.SendConfirmationEmailToAdminDataCollector(ApplicationName,FirstName,LastName,model.File,"Report.xlsx");
                   
        bool didErrorOccur = false;

        var _tempList = GetDataTableFromSpreadsheet(model.File.InputStream,false,out didErrorOccur);
    
        if (didErrorOccur)                                    
            return Json(new { success = false,excel = true,message = errors.ToString() });

        var list = ExcelRepository.Save(model,_tempList);

        MailClient.SendConfirmationEmailToUserDataCollector(user.Email,user.FirstName,user.LastName);                                         

        return Json(new { success = true,message = "success" });
    }
    catch(Exception e)
    {
        return Json(new { success = false,excel = false,message = e.Message });
    }
}

这是我的电子邮件方式

public static void SendConfirmationEmailToAdminDataCollector(string ApplicationName,string fname,string lname,HttpPostedFileBase document,string documentName)
{
        string strSmtpServer = smtp_server;
        string strSmtpPickup = smtp_pickup_location;
        int intPort = smtp_port;
        MailMessage objMail = new MailMessage();

        string email_to = !string.IsNullOrEmpty(enviroment) && enviroment == "dev" ? email_to_test : email_admin;           

        objMail.From = new MailAddress(email_from);
        MailClient.AddAddress(email_to,ADD_EMAIL_TO,ref objMail);
        MailClient.AddAddress(email_bcc,ADD_EMAIL_BCC,ref objMail);

        objMail.Subject = "New Submission From " + ApplicationName;
        objMail.Body =  "<p>A new submission was entered by " + fname + " " + lname + ".</p>" +
                        "<p>Thank You</p>";
        
        objMail.Attachments.Add(new Attachment(document.InputStream,documentName));
        objMail.IsBodyHtml = true;

        try
        {
            MailClient.sendMail(strSmtpServer,intPort,strSmtpPickup,objMail);
        }
        catch
        {
            throw new Exception("Could not send a confirmation email");
        }
}

发送电子邮件后,为什么model.File.ContentLength属性为0?

解决方法

我先更改了保存数据的顺序,然后发送电子邮件,最重要的是在使用流content.Seek(0,SeekOrigin.Begin);之后重置流的位置

这是有效的家庭控制器方法:

[HttpPost]
public ActionResult Index(ImportExcelViewModel model)
{                       
    try
    {
        var user = (User)Session[Utils.USER_SESSION];                
        var content = model.File.InputStream;                              

        bool didErrorOccur = false;

        var _tempList = GetDataTableFromSpreadsheet(content,false,out didErrorOccur);
    
        if (didErrorOccur)                                    
            return Json(new { success = false,excel = true,message = errors.ToString() });

        model.UserID = user.ID;

        content.Seek(0,SeekOrigin.Begin);

        var newEntry = DataCollectorRepository.Save(model,_tempList);

        content.Seek(0,SeekOrigin.Begin);

        if (newEntry != null)
        {
            string file_name = model.ApplicationName + ".xlsx";
                        MailClient.SendConfirmationEmailToAdminDataCollector(model.ApplicationName,user.FirstName,user.LastName,content,file_name);
            MailClient.SendConfirmationEmailToUserDataCollector(user.Email,user.LastName);
        }
        else
            return Json(new { success = false,excel = false,message = "Something went wrong saving your submission." });
    

        return Json(new { success = true,message = "success" });
    }
    catch(Exception e)
    {
        return Json(new { success = false,message = e.Message });
    }
}

版权声明:本文内容由互联网用户自发贡献,该文观点与技术仅代表作者本人。本站仅提供信息存储空间服务,不拥有所有权,不承担相关法律责任。如发现本站有涉嫌侵权/违法违规的内容, 请发送邮件至 dio@foxmail.com 举报,一经查实,本站将立刻删除。