您好, 欢迎来到 !    登录 | 注册 | | 设为首页 | 收藏本站

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

bubuko 2022/1/25 18:55:18 dotnet 字数 4630 阅读 983 来源 http://www.bubuko.com/infolist-5-1.html

web: ascx页面 <tr> <td> <asp:Label ID="Label7" runat="server" Text="上传文件:"></asp:Label> </td> <td> <asp:FileUpload ID="FileInfo" runat="server" /> </td> ...

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#文件上传到数据库

原文:https://www.cnblogs.com/liuguiqing/p/15218159.html


如果您也喜欢它,动动您的小指点个赞吧

除非注明,文章均由 laddyq.com 整理发布,欢迎转载。

转载请注明:
链接:http://laddyq.com
来源:laddyq.com
著作权归作者所有。商业转载请联系作者获得授权,非商业转载请注明出处。


联系我
置顶