摘要:[ASP.NET][FileUpload]範本
protected void Button_insert_Click(object sender, EventArgs e)
{
//1.檢查是否已選好檔案,若否,則停止上傳。
if (FileUpload1.HasFile == false)
{
MessageBox("", "請先挑選檔案之後,再來上傳!");
return;
}
//2.檢查副檔名是否允許
string fileName = FileUpload1.FileName; //-- User上傳的檔名(不包含 Client端的路徑!)
string fileExtension = Path.GetExtension(fileName).ToLower();//取得副檔名
List allowExtension = new List { ".jpg", ".bmp" };
if(allowExtension.IndexOf(fileExtension)==-1)
{
MessageBox("", "此檔案類型不被允許,請重新挑選!");
return;
}
//3.檢查檔案大小是否允許
int filesize = FileUpload1.PostedFile.ContentLength;//取得檔案大小
int allowsize = 500;// (K) //允許大小
if (filesize > allowsize)
{
Label1.Text = "您上傳的檔案大小是:" + filesize / 1024 + " K,必須小於: " + allowsize + " k";
return;
}
//4.檢查路徑是否存在,不存在就自動建立
string savePath = "e:\\uploads\\Led_play\\";
if (Directory.Exists(savePath) == false)
{
Directory.CreateDirectory(savePath);
}
//5.檢查檔名是否有同名
string savepathfilename = Path.Combine(savePath,fileName);//結合檔案路徑+檔名
string onlyfileName = Path.GetFileNameWithoutExtension(fileName);//取得檔名(不含副檔名)
int i = 1;
while (File.Exists(savepathfilename))
{
fileName = string.Concat(onlyfileName, "_", i, fileExtension);
savepathfilename = Path.Combine(savePath, fileName);
i++;
}
//6.完成檔案上傳的動作。
ViewState["savepathfilename"] = savepathfilename;
FileUpload1.SaveAs(savepathfilename);
Label1.Text = "上傳成功,檔名---- " + fileName + " ; 檔案大小: " + filesize / 1000 + " k";
//7.把檔案路徑寫入資料庫
insertfilepath();//副程式
}
private void insertfilepath()
{
string ds = WebConfigurationManager.ConnectionStrings["workerConnectionString"].ConnectionString;//不須引號
SqlConnection conn = new SqlConnection(ds);
string insql = @"insert into [temp_led]
([customer_erp],
[cuttomer_name],
[date_start],
[date_end],
[date_online],
[second],
[filepath],
[state],
[updatetime])
values(@customer_erp,
@cuttomer_name,
@date_start,
@date_end,
@date_online,
@second,
@filepath,
'y',
getdate())
";
SqlCommand cmd = new SqlCommand(insql, conn);
conn.Open();
cmd.Parameters.AddWithValue("@customer_erp", TextBox_erp.Text);
cmd.Parameters.AddWithValue("@cuttomer_name", TextBox_name.Text);
cmd.Parameters.AddWithValue("@date_start", TextBox_sd.Text);
cmd.Parameters.AddWithValue("@date_end", TextBox_ed.Text);
cmd.Parameters.AddWithValue("@date_online", TextBox_ondate.Text);
cmd.Parameters.AddWithValue("@filepath", ViewState["savepathfilename"].ToString());
cmd.Parameters.AddWithValue("@second", TextBox_second.Text);
cmd.ExecuteNonQuery();
conn.Close();
MessageBox("", "新增成功!");
ClientScript.RegisterStartupScript(ClientScript.GetType(), "myclear", "");
// Label_js.Text = "";
//Button_insert.Attributes.Add("OnClick", "javascript:myclear()");
}
public void MessageBox(string strKey, string strInfo)//方法:跳出警告框
{
if (!ClientScript.IsClientScriptBlockRegistered(strKey))
{
string strjs = "alert('" + strInfo + "');";
ClientScript.RegisterClientScriptBlock(this.GetType(), strKey, strjs, true);
}
}
--
強烈建議購物網店或實體店家都必須使用關鍵字廣告or原生廣告來 將Yahoo上與聯播網的廣大流量導至自己的網站!
●Yahoo關鍵字廣告/原生廣告
◆Yahoo廣告方案介紹 : https://goo.gl/5k8FHW
◆Yahoo廣告剖析與運用 : http://goo.gl/4xjUJD