All articles| All Pictures| All Softwares| All Video| Go home page| Write articles| Upload pictures

Reading number is top 10 articles
Cookie跨域、虚拟目录,,实现通行证登录_[Asp.Net教程]
通过实例学习C#开发中的泛型_.net资料_编程技术
安全基础之IIS,6,的,PHP,最佳配置方法_php资料_编程技术
理解ASP.NET与客户端缓存之HTTP协议_.net资料_编程技术
ASP.NET,2.0移动开发之设备筛选器的应用_[Asp.Net教程]
利用Visual,C#编程模拟鼠标操作_[Asp.Net教程]
delphi单选框组件(TRadioButton)使用实例
入门:Windows环境下PHP动态网页配置详解_php资料_编程技术
给.Net初学者的一些建议(共勉之)_[Asp.Net教程]
javascript网页特效:闪现的礼花背景_JavaScript技术_编程技术
Reading number is top 10 pictures
青春清纯美女大集合3
不知名的美女素颜照1
漂亮脸蛋魔鬼身材1
A man's favorite things15
Original author said, this is the Hengyang people
这只猪到底犯了什么错?
职场回春术
粉红蕾丝的美女
5 meters long centenarians python and melee was successfully capture king snake (figure)
Fierce! China's special forces training the devil1
Download software ranking
Unix video tutorial4
塘西风月痕
致我们终将逝去的青春
Eclipse 4.2.2 For Win64
Kung.Fu.Panda.2
打鸟视频
Boxer vs Yellow1
ASP.NET.2.0.XML.高级编程(第3版)
Be there or be square
XML+Web+Service开发教程
delv published in(发表于) 2014/1/27 6:51:01 Edit(编辑)
如何在ASP.NET中上传文件到数据库_[Asp.Net教程]

如何在ASP.NET中上传文件到数据库_[Asp.Net教程]

如何在ASP.NET中上传文件到数据库_[Asp.Net教程]

  上回谈到了如何下载文件,包含数据库中的文件,动态产生的文件...等。
这次我们来看看怎么把文件上传到数据库中,这边需要注意一个小地方,在上传文件到数据库的部分,数据栏位的性态若是Access请选择Ole对象,若是SQL Server请选择Image格式。


接着您可以通过下面的代码进行文件的上传:


If Me.FileUpload1.PostedFile.ContentLength > 0 Then
  Dim cn As New Data.OleDb.OleDbConnection
  Dim dc As Data.OleDb.OleDbCommand
  Dim para As Data.OleDb.OleDbParameter
  Dim SQL, FileName As String
  '连线字符串
  cn.ConnectionString = Me.AccessDataSource1.ConnectionString
  cn.Open()
  '运行 TextBox 中的 SQL 指令
  FileName = Me.FileUpload1.PostedFile.FileName
  SQL = "insert into FileStorage (FileName,FileBody) values ('" & FileName & "',?)"
  '建立 SqlCommand 对象
  dc = New Data.OleDb.OleDbCommand(sql, cn)
  para = New Data.OleDb.OleDbParameter("file", Data.OleDb.OleDbType.Binary)
  para.Value = Me.FileUpload1.FileBytes
  dc.Parameters.Add(para)
  '运行(通过 Parameters 将文件存储到数据库)
  dc.ExecuteNonQuery()
  ShowAlertMsg("上传文件存储完毕!")
Else
  ShowAlertMsg("上传文件不存在!")
End If


  上传至数据库中的文件,可以通过下面的方式重新下载回来,请特别这注意这张网页在调用的时候必须提供文件编号做为URL中的命令行参数:


  Me.AccessDataSource1.SelectCommand = "select * from FileStorage where uid=" & Request.QueryString("FileID")
  Dim dv As Data.DataView = Me.AccessDataSource1.Select(New DataSourceSelectArguments)
  '准备下载文件
  Response.ClearHeaders()
  Response.Clear()
  Response.Expires = 0
  Response.Buffer = True
  Dim fileName As String = dv.Item(0).Item("FileName")
  '通过 Header 设定文件名
  Response.AddHeader("content-disposition", "attachment; filename=" & Chr(34) & System.Web.HttpUtility.UrlEncode(IO.Path.GetFileName(fileName), System.Text.Encoding.UTF8) & Chr(34))
  Response.ContentType = "Application/octet-stream"
  '传出要让使用者下载的内容
  Response.BinaryWrite(dv.Item(0).Item("FileBody"))
  '释放资源
  Response.End()


整个范例请参考:
http://video.studyhost.com/Download/Example/ASP.NET/FileUploadAndDownloadWithDB.rar


来源:csdn







添加到del.icio.us 添加到新浪ViVi 添加到百度搜藏 添加到POCO网摘 添加到天天网摘365Key 添加到和讯网摘 添加到天极网摘 添加到黑米书签 添加到QQ书签 添加到雅虎收藏 添加到奇客发现 diigo it 添加到饭否 添加到飞豆订阅 添加到抓虾收藏 添加到鲜果订阅 digg it 貼到funP 添加到有道阅读 Live Favorites 添加到Newsvine 打印本页 用Email发送本页 在Facebook上分享


Disclaimer Privacy Policy About us Site Map

If you have any requirements, please contact webmaster。(如果有什么要求,请联系站长)
Copyright ©2011-
uuhomepage.com, Inc. All rights reserved.