把存储在SQL7的image字段的文件下载到客户端的ASP源代码

文 件 名:download.asp 
使用方法:download.asp?fid
= xxx
说  明:把SQL7的image字段存储的文件下载到客户端
数据库结构:[表名]tabimage {fid 
int   not   null ;filename varchar( 100 not   null ;filecontent image  not   null }
fid:文件id [PK];filename:文件名;filecontent:文件二进制内容


< %
Response.Buffer
= True
varfileid 
=  Request( " fid " )
If  varfileid = ""   Then
Response.write 
" 没有指定下载文件ID。 "
Response.End
End   If

OpenDB conn
SQL 
=   " SELECT filename,filecontent FROM tabimage WHERE fid= "   &  varfileid
Set  rs  =  conn.Execute(SQL)
If   Not  rs.Eof  Then
varfilename 
=  rs( " filename " )
varfilesize
= rs( " filecontent " ).ActualSize
varcontent 
=  rs( " filecontent " ).GetChunk(varfilesize)
Response.ContentType 
=   " */* "
Response.AddHeader 
" Content-Length " ,varfilesize
Response.AddHeader 
" Content-Disposition " " attachment;filename="" "   &  varfilename  &   " "" "
Response.binarywrite varcontent
End   If
rs.Close
Set  rs  =   Nothing
conn.Close
Set  conn  =   Nothing
Response.End

' 连接数据库通用过程
Sub  OpenDB (ByRef conn)
Set  conn  =  Server.CreateObject( " ADODB.Connection " )
conn.provider
= " sqloledb "
conn.ConnectionString 
=   " driver={SQL Server};server=xxx.xxx.xxx.xxx;uid=myusername;pwd=mypassword;database=mydatabase "
conn.Open
End Sub
%
>

 
  • 0
    点赞
  • 0
    收藏
    觉得还不错? 一键收藏
  • 0
    评论
ASP.NET 中,可以使用以下步骤将 FileUpload 控件中的图像和其他信息保存到 SQL Server 数据库中的 image 字段中: 1. 设置数据库连接字符串。 2. 在 ASP.NET 页面中添加 FileUpload 控件和其他输入控件(如文本框)来获取其他信息。 3. 在服务器代码中,使用 FileUpload 控件的 SaveAs 方法将图像保存到服务器上的某个文件夹中。 4. 使用 System.IO.File 类的 ReadAllBytes 方法将保存的图像读入字节数组中。 5. 创建 SQL Server 数据库连接并打开连接。 6. 创建 SQL INSERT 语句,将图像字节数组和其他信息插入到数据库中的 image 字段中。 7. 执行 SQL INSERT 语句并关闭数据库连接。 以下是一个示例代码: ```csharp protected void btnSubmit_Click(object sender, EventArgs e) { // 获取文件名和扩展名 string fileName = Path.GetFileName(FileUpload1.PostedFile.FileName); string fileExtension = Path.GetExtension(FileUpload1.PostedFile.FileName); // 保存文件到服务器上的某个文件夹中 string filePath = Server.MapPath("~/Images/") + fileName; FileUpload1.SaveAs(filePath); // 读入图像字节数组 byte[] imageData = File.ReadAllBytes(filePath); // 获取其他信息 string name = txtName.Text; string description = txtDescription.Text; // 创建数据库连接 using (SqlConnection connection = new SqlConnection("your_connection_string_here")) { // 打开连接 connection.Open(); // 创建 SQL INSERT 语句 string sql = "INSERT INTO MyTable (Name, Description, ImageData) VALUES (@Name, @Description, @ImageData)"; // 创建 SqlCommand 对象 using (SqlCommand command = new SqlCommand(sql, connection)) { // 添加参数 command.Parameters.AddWithValue("@Name", name); command.Parameters.AddWithValue("@Description", description); command.Parameters.AddWithValue("@ImageData", imageData); // 执行 SQL INSERT 语句 command.ExecuteNonQuery(); } // 关闭连接 connection.Close(); } } ``` 请注意,此示例代码仅供参考。您需要根据自己的实际情况对其进行修改。

“相关推荐”对你有帮助么?

  • 非常没帮助
  • 没帮助
  • 一般
  • 有帮助
  • 非常有帮助
提交
评论
添加红包

请填写红包祝福语或标题

红包个数最小为10个

红包金额最低5元

当前余额3.43前往充值 >
需支付:10.00
成就一亿技术人!
领取后你会自动成为博主和红包主的粉丝 规则
hope_wisdom
发出的红包
实付
使用余额支付
点击重新获取
扫码支付
钱包余额 0

抵扣说明:

1.余额是钱包充值的虚拟货币,按照1:1的比例进行支付金额的抵扣。
2.余额无法直接购买下载,可以购买VIP、付费专栏及课程。

余额充值