在会话中保存附件

Save an Attachment in session

我有一个 ASP.net 应用程序,在第 2 页 (pg2) 上,用户可以上传附件。在第 3 页 (pg3) 上,我确认了用户单击提交按钮的位置。然后,这会向我发送一封电子邮件,其中包含所有详细信息。此功能工作正常,但我没有收到附件,因为我不知道如何在会话中将它逐页传递。

第2页代码

下面的代码显示了我如何在会话中传递在第 2 页输入的详细信息

protected void pg2button_Click(object sender, EventArgs e)
{
    Session["pg2"] = txtData2.Text;
    Session["pg2Yes"] = pg2Yes.Checked ? "Yes" : "";
// CODE HERE TO PASS/STORE UPLOADED DOC
    Session["pg2No"] = pg2No.Checked ? "No" : "";
    Response.Redirect("/Quotation/pg3.aspx");
}

这是我的 HTML

<div class="form-group">         
     <asp:Label ID="Label3" class="col-md-3 control-label" runat="server" Text="Upload"></asp:Label>
     <div class="col-md-3">
          <asp:FileUpload ID="fuAttachment" runat="server" class="form-control"></asp:FileUpload>
     </div>
</div>

第3页代码

protected void pg3button_Click(object sender, EventArgs e)
{            
    try
    {
        //Create the msg object to be sent
        MailMessage msg = new MailMessage();

        //Add your email address to the recipients
        msg.To.Add("test@hotmail.co.uk");

        //Configure the address we are sending the mail from
        MailAddress address = new MailAddress("test@hotmail.co.uk");
        msg.From = address;

        //Append their name in the beginning of the subject
        msg.Subject = "Quote Requst";

        msg.Body = Label1.Text + " " + Session["pg1input"].ToString()
                    + Environment.NewLine.ToString() +
                    Label5.Text + " " + Session["emailinput"].ToString()
                    + Environment.NewLine.ToString() +
                    Label2.Text + " " + Session["pg1dd"].ToString()
                    +Environment.NewLine.ToString() +
                    Label3.Text + " " + Session["pg2"].ToString();

        //Configure an SmtpClient to send the mail.
        SmtpClient client = new SmtpClient("smtp.live.com", 587);
        client.EnableSsl = true; //only enable this if your provider requires it

        //Setup credentials to login to our sender email address ("UserName", "Password")
        NetworkCredential credentials = new NetworkCredential("test@hotmail.co.uk", "Password");
        client.Credentials = credentials;

        //Send the msg
        client.Send(msg);

        Response.Redirect("/Quotation/pg4.aspx");
    }
    catch
    {
        //If the message failed at some point, let the user know
        lblResult.Text = "<div class=\"form-group\">" + "<div class=\"col-xs-12\">" + "There was a problem sending your request. Please try again." + "</div>" + "</div>" + "<div class=\"form-group\">" + "<div class=\"col-xs-12\">" + "If the error persists, please contact us." + "</div>" + "</div>";
    }
}

下面的link我可以开始工作,但前提是上传字段在同一页面上 http://www.aspsnippets.com/Articles/How-to-send-email-with-attachment-in-ASPNet.aspx

如果您将路径存储在会话中,则可以使用此方法获取文件的字节数:

private byte[] GetFileBytes(string myPath)
{
    FileInfo file = new FileInfo(myPath);
    byte[] bytes = new byte[file.Length];
    using (FileStream fs = file.OpenRead())
    {
        fs.Read(bytes, 0, bytes.Length);
    }
    return bytes;
}

获取文件内容并存储在session中(重定向前添加到pg2点击事件):

var file = fuAttachment.PostedFile;
if (file != null)
{
    var content = new byte[file.ContentLength];
    file.InputStream.Read(content, 0, content.Length);
    Session["FileContent"] = content;
    Session["FileContentType"] = file.ContentType;
}

下载此文件:

Response.Clear();
Response.ClearHeaders();
Response.ClearContent();
Response.AppendHeader("content-length", ((byte[])Session["FileContent"]).Length);
Response.ContentType = (string)Session["FileContentType"];
Response.AppendHeader("Content-Disposition", "attachment; filename=fileName");
Response.BinaryWrite((byte[])Session["FileContent"]);

HttpContext.Current.ApplicationInstance.CompleteRequest();

在 pg3 附加存储文件:

var contentStream = new MemoryStream((byte[]) Session["FileContent"]);
msg.Attachments.Add(new Attachment(contentStream,"file.ext",(string) Session["FileContentType"])); // Or store file name to Session for get it here.