protected void Upload_Click(object sender, EventArgs e)
{
String path = Server.MapPath("../Document/");
String Introduction = "";
if (up.HasFile)
{
if (up.FileContent.Length <= 4000 * 1024)
{
SqlConnection conn = new SqlConnection("Data Source=.;Initial Catalog=jiaoxue;Integrated Security=True");
conn.Open();
SqlCommand cmd = new SqlCommand("select count(*) from Document where doc_name='" + up.FileName.ToString().Trim() + "'", conn);
int i = Convert.ToInt32(cmd.ExecuteScalar());
conn.Close();
if (i > 0)
{
MessageBox.Show("您上傳的文件或文件名以存在,請重新確認後再上傳!");
return;
}
if (introduction.Text.ToString().Trim() == "")
{
Introduction = "暫無對該文件的說明!";
}
else
{
Introduction = introduction.Text.ToString().Trim();
}
introduction.Text = "";
up.PostedFile.SaveAs(path + up.FileName);
long size = up.FileContent.Length / 1024;
string filesize = size.ToString() + " k";
conn.Open();
cmd = new SqlCommand("insert into Document(user_name,doc_name,doc_size,doc_address,upload_time,introduction) values('" + Session["Username"].ToString().Trim() + "','" + up.FileName.ToString().Trim() + "','" + filesize.Trim() + "','" + path.ToString().Trim() + "','" + System.DateTime.Now.ToString() + "','" + Introduction + "')", conn);
cmd.ExecuteNonQuery();
conn.Close();
Page.ClientScript.RegisterStartupScript(GetType(), "", "<script>alert('上傳成功!')</script>");
return;
}
else
{
MessageBox.Show("對不起,由於您的文件過大無法上傳!");
return;
}
}
else
{
Page.ClientScript.RegisterStartupScript(GetType(), "", "<script>alert('您還沒有選擇要上傳文件!')</script>");
return;
}
}
//這個允許上傳最大4M的文件,妳可以試壹下