C#文件上传及查看,C#文件上传到数据库

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);
}

  

 

C#文件上传及查看,C#文件上传到数据库

上一篇:Oracle行转列(使用pivot函数)


下一篇:微博Rss邮箱推送系统