CSDN首页 空间 新闻 论坛 Blog 下载 读书 网摘 搜索 .NET Java 视频 接项目 求职 在线学习 买书 程序员 通知
可用分押宝游戏火热进行中... 专题改版:Java Web 专题
CSDN社区
搜索 收藏 打印 关闭
CSDN社区 >  .NET技术 >  ASP.NET

文档上传到服务器的某个文件目录下,数据库中只存该文档的路径

楼主kensli()2003-12-02 09:51:36 在 .NET技术 / ASP.NET 提问

那位有这个的原代码,我不知道该如何作。 问题点数:20、回复次数:4Top

1 楼kensli()回复于 2003-12-02 10:06:37 得分 0

大侠帮忙!Top

2 楼Sunmast(速马@Redmond, WA)回复于 2003-12-02 10:23:47 得分 10

using   System;  
  using   System.Collections;  
  using   System.ComponentModel;  
  using   System.Data;  
  using   System.Data.SqlClient;  
  using   System.Drawing;  
  using   System.Text;  
  using   System.Web;  
  using   System.Web.SessionState;  
  using   System.Web.UI;  
  using   System.Web.UI.WebControls;  
  using   System.Web.UI.HtmlControls;  
  using   System.IO;  
  using   Uestc.News.Data;  
   
  namespace   Uestc.News.Admin  
  {  
  ///   <summary>  
  ///   UploadImage   的摘要说明。  
  ///   Tested   by   Sunmast   at   26,Nov,2003,18:12  
  ///   </summary>  
  public   class   UploadFile   :   Common  
  {  
  protected   System.Web.UI.WebControls.Literal   existImages;  
  protected   System.Web.UI.HtmlControls.HtmlInputButton   useCurrent;  
  protected   System.Web.UI.HtmlControls.HtmlInputButton   cancel;  
  private   System.Web.HttpPostedFile   oFile;  
  private   string   name;  
  private   int   size;  
   
  private   void   Page_Load(object   sender,   System.EventArgs   e)  
  {  
  oFile   =   Request.Files.Get("FCKeditor_File")   ;  
  if(oFile   ==   null)  
  return;  
  else  
  Session["imgFile"]   =   oFile;  
   
  //   检查数据库是否已记录可能重复的文件  
  size   =   oFile.ContentLength;  
  name   =   oFile.FileName;  
  name   =   name.Remove(0,name.LastIndexOf('\\')   +   1);  
   
  SqlConnection   conn   =   Data.Common.GetConn();  
  string   sqlQuery   =   "SELECT   *   FROM   Image   WHERE   FileSize   =   "   +   size.ToString()   +   "   OR   FileName   =   '"   +   name   +   "'";  
  DataRowCollection   drc   =   News.Data.SqlHelper.ExecuteDataset(conn,CommandType.Text,sqlQuery).Tables[0].Rows;  
  conn.Close();  
   
  if(drc.Count   >   0)  
  {  
  StringBuilder   sb   =   new   StringBuilder();  
  foreach(DataRow   dr   in   drc)  
  {  
  int   X   =   Convert.ToInt32(dr["Width"]);  
  int   Y   =   Convert.ToInt32(dr["Height"]);  
  float   yx   =   (float)Y   /   (float)X;  
  if(yx   >   0.75)  
  {  
  if(Y   >   300)  
  {  
  Y   =   300;  
  X   =   (int)System.Math.Round(Y   /   yx);  
  }  
  }  
  else  
  {  
  if(X   >   400)  
  {  
  X   =   400;  
  Y   =   (int)System.Math.Round(X   *   yx);  
  }  
  }  
  sb.Append("<a   href=\"javascript:getImage('"   +   dr["FilePath"].ToString()   +   "',"   +   X.ToString()   +   ","   +   Y.ToString()   +   ");\">"   +   dr["FileName"].ToString()   +   "</a><br/>");  
  }  
  existImages.Text   =   sb.ToString();  
  return;  
  }  
  else  
  {  
  SaveFile(oFile);  
  return;  
  }  
  }  
   
  private   void   SaveFile(HttpPostedFile   oFile)  
  {  
  //   保存文件并添加记录到数据库  
  size   =   oFile.ContentLength;  
  name   =   oFile.FileName;  
  name   =   name.Remove(0,name.LastIndexOf('\\')   +   1);  
  string   today   =   System.DateTime.Now.Date.ToString();  
  today   =   today.Substring(0,today.IndexOf('   '));  
   
  string   sFileURL     =   "/Data/Image/"   +   today   +   "/";  
  DirectoryInfo   di   =   new   DirectoryInfo(Server.MapPath(sFileURL));  
  if(!di.Exists)  
  di.Create();  
   
  sFileURL   =   sFileURL   +   System.DateTime.Now.TimeOfDay.ToString().Replace(":",".")   +   System.IO.Path.GetExtension(oFile.FileName);  
  string   sFilePath   =   Server.MapPath(sFileURL);  
  oFile.SaveAs(sFilePath);  
  System.Drawing.Image   img   =   System.Drawing.Image.FromFile(sFilePath);  
  int   width   =   img.Width;  
  int   height   =   img.Height;  
  img.Dispose();  
   
  SqlConnection   conn   =   Data.Common.GetConn();  
  string   sqlQuery   =   "INSERT   INTO   Image   (FileName,   FilePath,   FileSize,   Width,   Height,   UploadTime)   VALUES   ('"    
  +   name   +   "',   '"   +   sFileURL   +   "',   "   +   size.ToString()   +   ","   +   width.ToString()   +   ","   +   height.ToString()   +   ",   '"   +   System.DateTime.Now.ToString()   +   "')";  
  News.Data.SqlHelper.ExecuteNonQuery(conn,CommandType.Text,sqlQuery);  
  conn.Close();  
  Session["imgFile"]   =   null;  
   
  Response.Write("<SCRIPT   language=javascript>window.opener.setImage('"   +   sFileURL   +   "')   ;   window.close();</"   +   "SCRIPT>");  
  return;  
  }  
   
  protected   void   useCurrentImg(object   Source,   EventArgs   e)  
  {  
  SaveFile((HttpPostedFile)Session["imgFile"]);  
  return;  
  }  
   
  protected   void   cancelAll(object   Source,   EventArgs   e)  
  {  
  Session["imgFile"]   =   null;  
  Response.Write("<SCRIPT   language=javascript>window.opener.setImage('')   ;   window.close();</"   +   "SCRIPT>");  
  return;  
  }  
   
  #region   Web   窗体设计器生成的代码  
  override   protected   void   OnInit(EventArgs   e)  
  {  
  //  
  //   CODEGEN:   该调用是   ASP.NET   Web   窗体设计器所必需的。  
  //  
  InitializeComponent();  
  base.OnInit(e);  
  }  
   
  ///   <summary>  
  ///   设计器支持所需的方法   -   不要使用代码编辑器修改  
  ///   此方法的内容。  
  ///   </summary>  
  private   void   InitializeComponent()  
  {          
  this.Load   +=   new   System.EventHandler(this.Page_Load);  
   
  }  
  #endregion  
  }  
  }Top

3 楼Sunmast(速马@Redmond, WA)回复于 2003-12-02 10:27:22 得分 0

上面是我给FCKEditor重写的图片上传页面  
  包括了你要的上传文件并记录到数据库的要求Top

4 楼asnowranger(asnowranger)回复于 2003-12-02 10:28:38 得分 10

<%@   Page   language="c#"%>  
  <%@   Import   Namespace="System.Data.SqlClient"   %>  
  <%@   Import   Namespace="System.Web"   %>  
  <%@   Import   Namespace="System.Web.UI"   %>  
  <%@   Import   Namespace="System.Web.UI.WebControls"   %>  
  <%@   Import   Namespace="System.Data"   %>  
  <script   runat="server">  
  override   protected   void   OnInit(EventArgs   e)  
  {  
  this.btUp.Click   +=   new   System.EventHandler(this.btUp_Click);  
  this.DataGrid1.DeleteCommand   +=   new   System.Web.UI.WebControls.DataGridCommandEventHandler(this.DataGrid1_DeleteCommand);  
  this.Load   +=   new   System.EventHandler(this.Page_Load);  
  base.OnInit(e);  
  }  
  void   Page_Load(object   sender,   System.EventArgs   e)  
  {  
  this.btUp.Attributes.Add("onclick","loading.style.display='';document.body.enabled=true");  
  if(Request["downFile"]!=null)//下载文件  
  {  
  string   strFileID   =   Request["downFile"];  
  SqlConnection   conn   =   new   SqlConnection("Data   Source=localhost;database=test;user   id=sa;password=");  
  SqlDataAdapter   adapt   =   new   SqlDataAdapter("select   *   from   [file]   where   id="+strFileID,conn);  
  DataSet   ds   =   new   DataSet();  
  adapt.Fill(ds);  
  if(ds.Tables.Count!=0   &&   ds.Tables[0].Rows.Count!=0)  
  {  
  Response.ClearHeaders();  
  Response.ClearContent();  
  Response.AddHeader("Content-Disposition",   "attachment;   filename="+ds.Tables[0].Rows[0]["fileName"].ToString());  
  byte[]   context   =   (Byte[])ds.Tables[0].Rows[0]["fileContext"];  
  Response.OutputStream.Write(context,0,context.Length);  
  Response.End();  
  }  
  else  
  {  
  Response.Write("<font   color=red>没有找到要下载的文件</font>");  
  }  
  }  
  else //显示文件列表  
  {  
  ListFile();  
  }  
  }  
  void   ListFile()  
  {  
  SqlConnection   conn   =   new   SqlConnection("Data   Source=localhost;database=test;user   id=sa;password=");  
  SqlDataAdapter   adapt   =   new   SqlDataAdapter("select   *   from   [file]",conn);  
  DataSet   ds   =   new   DataSet();  
  adapt.Fill(ds);  
  this.DataGrid1.DataSource   =   ds;  
  this.DataGrid1.DataBind();  
  }  
  void   btUp_Click(object   sender,   System.EventArgs   e)  
  {  
  if(File.PostedFile!=null   &&   File.PostedFile.ContentLength!=0)  
  {  
  int   size   =   File.PostedFile.ContentLength;  
   
  SqlConnection   conn   =   new   SqlConnection("Data   Source=localhost;database=test;user   id=sa;password=");  
  SqlCommand   cmd   =   new   SqlCommand("insert   into   [file]   (fileName,fileType,fileSize,fileContext)   values(@fileName,@fileType,@fileSize,@fileContext)",conn);  
  //文件名  
  SqlParameter   param   =   new   SqlParameter("@fileName",SqlDbType.VarChar,50);  
  param.Value   =   File.PostedFile.FileName.Substring(File.PostedFile.FileName.LastIndexOf("\\")+1);  
  cmd.Parameters.Add(param);  
  //文件类型  
  param   =   new   SqlParameter("@fileType",SqlDbType.VarChar,50);  
  param.Value   =   File.PostedFile.ContentType;  
  cmd.Parameters.Add(param);  
  //文件大小  
  param   =   new   SqlParameter("@fileSize",SqlDbType.Float,8);  
  param.Value   =   size;  
  cmd.Parameters.Add(param);  
  //文件内容  
  byte[]   context   =   new   Byte[size];  
  param   =   new   SqlParameter("@fileContext",SqlDbType.Image);  
  File.PostedFile.InputStream.Read(context,0,size);  
  param.Value   =   context;  
  cmd.Parameters.Add(param);  
   
  conn.Open();  
  cmd.ExecuteNonQuery();  
  conn.Close();  
   
  ListFile();  
  }  
  else  
  {  
  Response.Write("<font   color=red>上传文件为空</font>");  
  }  
  }  
  void   DataGrid1_DeleteCommand(object   source,   System.Web.UI.WebControls.DataGridCommandEventArgs   e)  
  {  
  SqlConnection   conn   =   new   SqlConnection("Data   Source=localhost;database=test;user   id=sa;password=");  
  SqlCommand   cmd   =   new   SqlCommand("delete   [file]   where   id   =   @id",conn);  
  SqlParameter   param   =   new   SqlParameter("@id",SqlDbType.Int);  
  param.Value   =   e.Item.Cells[0].Text;  
  cmd.Parameters.Add(param);  
  conn.Open();  
  cmd.ExecuteNonQuery();  
  conn.Close();  
   
  ListFile();  
  }  
  </script>  
  <HTML>  
  <HEAD>  
  <title>通用上传下载--支持所有格式文件</title>  
  </HEAD>  
  <body>  
  <form   id="WebForm5"   method="post"   runat="server"   enctype="multipart/form-data">  
  <input   id="File"   type="file"   runat="server">  
  <asp:button   id="btUp"   runat="server"   Text="上     传"></asp:button><asp:datagrid   id="DataGrid1"   runat="server"   AutoGenerateColumns="False">  
  <HeaderStyle   BackColor="#669966"></HeaderStyle>  
  <Columns>  
  <asp:BoundColumn   DataField="ID"   HeaderText="ID">  
  <HeaderStyle   HorizontalAlign="Center"></HeaderStyle>  
  </asp:BoundColumn>  
  <asp:BoundColumn   DataField="fileName"   HeaderText="文件名">  
  <HeaderStyle   HorizontalAlign="Center"></HeaderStyle>  
  </asp:BoundColumn>  
  <asp:BoundColumn   DataField="fileType"   HeaderText="文件类型">  
  <HeaderStyle   HorizontalAlign="Center"></HeaderStyle>  
  </asp:BoundColumn>  
  <asp:BoundColumn   DataField="fileSize"   HeaderText="文件大小">  
  <HeaderStyle   HorizontalAlign="Center"></HeaderStyle>  
  </asp:BoundColumn>  
  <asp:TemplateColumn>  
  <ItemTemplate>  
  <asp:HyperLink   Text="下载"   NavigateUrl='<%#   String.Format("{0}?downFile={1}",Request.CurrentExecutionFilePath,DataBinder.Eval(Container.DataItem,"ID"))%>'   Runat="server">  
  </asp:HyperLink>  
  </ItemTemplate>  
  </asp:TemplateColumn>  
  <asp:ButtonColumn   Text="删除"   CommandName="Delete"></asp:ButtonColumn>  
  </Columns>  
  </asp:datagrid></form>  
  <div   id="loading"   style="display:none;border-left:1px   solid   #EEEEEE;border-top:1px   solid   #EEEEEE;border-right:1px   solid   #666666;border-bottom:1px   solid   #666666;background-color:#DDDDDD;width:300px;heigth:200px;text-align:center">  
  正在进行操作,请稍候……  
  </div>  
  </body>  
  </HTML>Top

相关问题

  • word文档上传到数据库中
  • 数据库路径问题
  • 怎样实现把图片的路径传到数据库,再把图片显示到浏览器
  • 高分求教!XML文档传到数据库时的参数选择!高难度,多多参与!!!
  • 数据库安装路径问题?
  • 分发BDE时的数据库路径
  • 数据库路径问题,急!
  • 数据库的路径问题
  • 数据库路径用不用修改?
  • 数据库中如何实现路径

关键词

  • 文档

得分解答快速导航

  • 帖主:kensli
  • Sunmast
  • asnowranger

相关链接

  • CSDN .NET频道
  • .NET类图书
  • C#类图书
  • .NET类源码下载

广告也精彩

反馈

请通过下述方式给我们反馈
反馈
提问
网站简介|广告服务|VIP资费标准|银行汇款帐号|网站地图|帮助|联系方式|诚聘英才|English|问题报告
世纪乐知(北京)网络技术有限公司 版权所有, 京 ICP 证 020026 号
北京创新乐知广告有限公司 提供技术支持
Copyright © 2000-2007, CSDN.NET, All Rights Reserved
GongshangLogo