.net簡單的fileupload控件上傳


前台代碼:

<asp:FileUpload ID="FileUpload1" runat="server" />
<asp:Button ID="filebut" runat="server" Text="上傳" onclick="filebut_Click" />

后台代碼:

protected void filebut_Click(object sender, EventArgs e)
{
if (FileUpload1.PostedFile.FileName == "")
{
Response.Write("<script>alert('請選擇上傳文件!')</script>");
}
else
{

HttpFileCollection uploadfiles = Request.Files;
string filetiem = DateTime.Now.ToString();//當前時間
string fileusername = Session.Contents["userid"].ToString();//當前用戶
string gongwid = Request.QueryString["id"];//文章id

for (int i = 0; i < uploadfiles.Count; i++)
{
HttpPostedFile mypost = uploadfiles[i];
try
{
string filepath = mypost.FileName;
string filename = filepath.Substring(filepath.LastIndexOf("\\") + 1);//獲取文件名
string serverpath = Server.MapPath("UploadFile\\") + filename;//服務器上傳地址
if (System.IO.File.Exists(serverpath))
{
Response.Write("<script>alert('文件已存在請重新命名!')</script>");
}
else
{
mypost.SaveAs(serverpath);
string filestrsql = "insert into OA_FILE(wjlj,wjm,gongwid,scr,scsj)values('" + serverpath + "','" + filepath + "','" + gongwid + "','" + fileusername + "','" + filetiem + "')";//sql執行語句
int sfcg = SqlHelper.wjsc(filestrsql);
if (sfcg > 0)
{
labzt.Text = "文件" + i + "上傳成功!路徑:" + serverpath + "\r\n";
}
}

}
catch (System.Exception ex)
{
labzt.Text = "上傳錯誤!原因:" + ex.Message.ToString();
}
}
}

}

由於先前是准備多加價格fieldupload控件的,但是不是特別美觀,但是實現了上傳的功能。


免責聲明!

本站轉載的文章為個人學習借鑒使用,本站對版權不負任何法律責任。如果侵犯了您的隱私權益,請聯系本站郵箱yoyou2525@163.com刪除。



 
粵ICP備18138465號   © 2018-2025 CODEPRJ.COM