在会话中保存附件

本文关键字:保存 会话 | 更新日期: 2023-09-27 18:07:04

我有一个ASP.net应用程序,在页面#2 (pg2)上用户可以上传一个附件。在页面#3 (pg3)是我的确认,用户点击一个提交按钮。然后,它会向我发送一封包含所有细节的电子邮件。这个功能工作得很好,但我没有得到附件,因为我不知道如何在会话中传递它的页面到页面。

Page 2 Code

下面的代码显示了我如何在会话

中传递第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>

Page 3 Code

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>";
    }
}

下面的链接,我可以得到的工作,但只有当上传字段是在同一页上http://www.aspsnippets.com/Articles/How-to-send-email-with-attachment-in-ASPNet.aspx

在会话中保存附件

获取文件内容并存储在会话中(在重定向之前添加到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();

At 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.

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

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