使用fileupload ASP.NET SQL Server提交表单的成功消息? [英] Success message for form submit with fileupload ASP.NET SQL server?
本文介绍了使用fileupload ASP.NET SQL Server提交表单的成功消息?的处理方法,对大家解决问题具有一定的参考价值,需要的朋友们下面随着小编来一起学习吧!
问题描述
此代码非常有效。但是当文件上传代码存在时,我没有收到Saved消息。我已经尝试提交一个简单的文本表单并获得成功消息,但不是当fileupload代码存在时。
protected void Button1_Click(object sender,EventArgs e)
{
string constr = ConfigurationManager.ConnectionStrings [DefaultConnection]。ConnectionString;
using(SqlConnection con = new SqlConnection(constr))
{
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 query =INSERT INTO LeaveReqTable(Name,EPFNo,FileName,ContentType,Data)VALUES(@ Name,@ EPFNo,@ FileName,@ ContentType,@ Data);
使用(SqlCommand cmd = new SqlCommand(query))
{
cmd.Connection = con;
con.Open();
cmd.Parameters.AddWithValue(@ Name,TextBox1.Text);
cmd.Parameters.AddWithValue(@ EPFNo,TextBox2.Text);
cmd.Parameters.AddWithValue(@ FileName,filename);
cmd.Parameters.AddWithValue(@ ContentType,contenttype);
cmd.Parameters.AddWithValue(@ Data,bytes);
int k = cmd.ExecuteNonQuery();
if(k!= 0)
{
Label1.Text =记录成功插入数据库;
Label1.ForeColor = System.Drawing.Color.CornflowerBlue;
ScriptManager.RegisterClientScriptBlock(this,this.GetType(),alertMessage,alert('Record Inserted Successfully'),true);
}
con.Close();
}
}
}
Response.Redirect(Request.Url.AbsoluteUri);
ScriptManager.RegisterClientScriptBlock(this,this.GetType(),alertMessage,alert('Record Inserted Successfully'),true);
}
}
我的尝试:
我已经尝试提交一个简单的文本表单并获得成功消息,但是当文件上载代码存在时却没有。
解决方案
不,你不会。
你在做一个Response.Redirect之后立即添加一条警告消息 - 后来发送一个HTTP请求发送该请求的浏览器以新页面响应的Web服务器 - 您添加或添加到旧页面的任何内容(例如您的警报)将被忽略,因为它已被新页面请求取代。
This code works very well. But I don't get Saved message when the file upload code is there. I've tried submitting a simple text form and I get success message, but not when fileupload code is there.
protected void Button1_Click(object sender, EventArgs e) { string constr = ConfigurationManager.ConnectionStrings["DefaultConnection"].ConnectionString; using (SqlConnection con = new SqlConnection(constr)) { 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 query = "INSERT INTO LeaveReqTable (Name, EPFNo, FileName, ContentType, Data) VALUES (@Name, @EPFNo, @FileName, @ContentType, @Data)"; using (SqlCommand cmd = new SqlCommand(query)) { cmd.Connection = con; con.Open(); cmd.Parameters.AddWithValue("@Name", TextBox1.Text); cmd.Parameters.AddWithValue("@EPFNo", TextBox2.Text); cmd.Parameters.AddWithValue("@FileName", filename); cmd.Parameters.AddWithValue("@ContentType", contenttype); cmd.Parameters.AddWithValue("@Data", bytes); int k = cmd.ExecuteNonQuery(); if (k != 0) { Label1.Text = "Record Inserted Succesfully into the Database"; Label1.ForeColor = System.Drawing.Color.CornflowerBlue; ScriptManager.RegisterClientScriptBlock(this, this.GetType(), "alertMessage", "alert('Record Inserted Successfully')", true); } con.Close(); } } } Response.Redirect(Request.Url.AbsoluteUri); ScriptManager.RegisterClientScriptBlock(this, this.GetType(), "alertMessage", "alert('Record Inserted Successfully')", true); } }
What I have tried:
I've tried submitting a simple text form and I get success message, but not when fileupload code is there.
解决方案
Well no, you won't.
You add an alert message immediately after doing a Response.Redirect - and the later sends an HTTP request to the browser which sends that request to the web server which responds with a new page - anything you add or added to the "old" page (such as your alert) is ignored because it has been "superseded" by the new page request.
这篇关于使用fileupload ASP.NET SQL Server提交表单的成功消息?的文章就介绍到这了,希望我们推荐的答案对大家有所帮助,也希望大家多多支持IT屋!
查看全文