在会话中保存附件

时间:2015-08-10 08:08:04

标签: c# asp.net webforms

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

Page 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>

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

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

2 个答案:

答案 0 :(得分:3)

获取文件内容并存储在会话中(在重定向之前添加到pg2 click事件):

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.

答案 1 :(得分:0)

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

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