上传按钮点击事件的数据

时间:2014-12-03 10:52:16

标签: c# asp.net web-applications

我是编程新手。我希望能够在我上传文件时保存到我的数据库中的网站上传文件,并发送我上传到其他两方的文件的电子邮件 我怎样才能实现这个目标?

这是我目前拥有的代码但我在当前上下文中不存在文件名错误?

protected void Upload(object sender, EventArgs e)
{
    string filename = Path.GetFileName(FileUpload1.PostedFile.FileName);
    string contentType = FileUpload1.PostedFile.ContentType;
    using (Stream fs = FileUpload1.PostedFile.InputStream)
    {
        using (BinaryReader br = new BinaryReader(fs))
        {
            byte[] bytes = br.ReadBytes((Int32)fs.Length);
            string constr = ConfigurationManager.ConnectionStrings["constr"].ConnectionString;
            using (SqlConnection con = new SqlConnection(constr))
            {
                string query = "insert into tblFiles values (@Name, @ContentType, @Data)";
                using (SqlCommand cmd = new SqlCommand(query))
                {
                    cmd.Connection = con;
                    cmd.Parameters.AddWithValue("@File Name", filename);
                    cmd.Parameters.AddWithValue("@ContentType", contentType);
                    cmd.Parameters.AddWithValue("@Data", bytes);
                    con.Open();
                    cmd.ExecuteNonQuery();
                    con.Close();
                }
            }
        }
    }
    Response.Redirect(Request.Url.AbsoluteUri);
}

protected void Upload_Bt1_Click(object sender, EventArgs e)
{
    MailMessage mailObj = new MailMessage(
    txtFrom.Text, txtTo.Text, txtSubject.Text, txtBody.Text);
    SmtpClient SMTPServer = new SmtpClient("127.0.0.1");
    try
    {
        SMTPServer.Send(mailObj);
    }
    catch (Exception ex)
    {
        Label1.Text = ex.ToString();
    }
}

0 个答案:

没有答案