心動一下:
web: ascx頁面
<tr> <td> <asp:Label ID="Label7" runat="server" Text="上傳文件:"></asp:Label> </td> <td> <asp:FileUpload ID="FileInfo" runat="server" /> </td> <td> <asp:Label ID="lblFileDescription" runat="server" Text="文件備注或說明:"></asp:Label> </td> <td> <asp:TextBox ID="tbxFileRemarks" runat="server" Width="254px"></asp:TextBox> <asp:Button ID="btnUploadFile" runat="server" OnClick="btnUploadFile_Click" Text="上傳" /> <asp:Label ID="filemessage" runat="server"></asp:Label> </td> </tr>
action: ascx.cs
protected void btnUploadFile_Click(object sender, EventArgs e)
{
if (FileInfo.HasFile)//上傳文件
{
string FileURL = "";
//文件路徑加上時間格式(可以封裝時間再調用),SaveAs為保存到具體文件夾中
FileURL = Server.MapPath("~/ContentFiles/") + tbxProjectInfoNo.Text + DateTime.Now.Year.ToString("00")
+ DateTime.Now.Month.ToString("00") + DateTime.Now.Day.ToString("00") + DateTime.Now.Hour.ToString("00") +
DateTime.Now.Minute.ToString("00") + DateTime.Now.Second.ToString("00") +
System.IO.Path.GetExtension(FileInfo.FileName);
FileInfo.SaveAs(FileURL);
//獲取保存的路徑
FileURL = @"http://" + this.PortalAlias.HTTPAlias + "/ContentFiles/" + tbxProjectInfoNo.Text + DateTime.Now.Year.ToString("00")
+ DateTime.Now.Month.ToString("00") + DateTime.Now.Day.ToString("00") + DateTime.Now.Hour.ToString("00") +
DateTime.Now.Minute.ToString("00") + DateTime.Now.Second.ToString("00") + System.IO.Path.GetExtension(FileInfo.FileName);
string filenames = "";
if (tbxFileRemarks.Text.Length>0) {
filenames = tbxFileRemarks.Text;
} else {
filenames = FileInfo.FileName;
}
cc.insertNewFileUrlAndInfo(FileURL, tbxProjectInfoNo.Text, filenames, UserInfo.Username);//保存路徑及提交人、項目編號、文件名稱到數據庫
dtProjectFileinfo = cc.GetProjectFiles(tbxProjectInfoNo.Text);//根據項目編號獲取信息
if (dtProjectFileinfo.Rows.Count > 0)
{
filemessage.Visible = true;
filemessage.ForeColor = System.Drawing.Color.Green;
filemessage.Text = "文件上傳成功。";
}
Page_Load(null, null);
}
}
數據封裝層:
/// <summary>
/// 獲取項目附件列表
/// </summary>
/// <param name="ProjectNo">項目號</param>
/// <returns></returns>
public DataTable GetProjectFiles(string ProjectNo)
{
return sqlAccess.ExecuteTable("select * from OverSystems_ProjectFiles where ProjectNo='" + ProjectNo + "'");
}
/// <summary>
/// 添加新文件
/// </summary>
/// <param name="FilePath">文件路徑</param>
/// <param name="ProjectID">項目號</param>
/// <param name="FileRemarks">文件描述</param>
public void insertNewFileUrlAndInfo(string FilePath, string ProjectID, string FileRemarks, string CreateUserName)
{
CommandText = "Update OverSystems_ProjectInfo Set IsContentFile=1 where ProjectNo='" + ProjectID + "'";
sqlAccess.ExecuteNonQuery(CommandText);
CommandText = "INSERT INTO OverSystems_ProjectFiles (ProjectNo,FilePath,FileRemarks,CreateDate,CreateUserName) " +
"VALUES('" + ProjectID + "','" + FilePath + "','" + FileRemarks + "',getdate(),'" + CreateUserName + "')";
sqlAccess.ExecuteNonQuery(CommandText);
}
