如何将放在数据库中的Word文件还原成XX.doc文件?

losting 2003-09-14 10:17:58
各位:
我将数据库中的Word文件输出时(用Response.binwrite()),发现word文檔为空?请高人指点呀!好多天了!谢谢!
...全文
53 9 打赏 收藏 转发到动态 举报
写回复
用AI写文章
9 条回复
切换为时间正序
请发表友善的回复…
发表回复
xjcrand 2003-09-15
  • 打赏
  • 举报
回复
输出时加入如下行:
Response.AppendHeader("Content-Disposition","attachment;
filename="+HttpUtility.UrlEncode("xxx.doc",System.Text.Encoding.UTF8));
yjtc 2003-09-15
  • 打赏
  • 举报
回复
请在vs.net动态帮助中查找 BLOB 关键字,会有一个很好的例子。
losting 2003-09-15
  • 打赏
  • 举报
回复
谢谢: acewang(**^o^**) ,但因我用ODBC,不用使用<%@import namespace="Microsoft.Data.Odbc"%>(说我没有引用),改为代码后置变为:

showfile.aspx 的内容:
<%@ Page language="c#" Codebehind="showfile.aspx.cs AutoEventWireup="false" Inherits="WordApplication.showfile" %>
(仅此一句)
showfile.aspx.cs的内容:
using System;
using System.Collections;
using System.ComponentModel;
using System.Data;
using System.Drawing;
using System.Web;
using System.Web.SessionState;
using System.Web.UI;
using System.Web.UI.WebControls;
using System.Web.UI.HtmlControls;
using Microsoft.Data.Odbc;
namespace WordApplication
{
/// <summary>
/// Summary description for showfile.
/// </summary>
public class showfile : System.Web.UI.Page
{
private void Page_Load(object sender, System.EventArgs e)
{
string strFileName="myouttest.doc";
string sql="SELECT fileid, filetype,imgSize,imgData FROM t_imgs WHERE fileid = 'testid2'";
Microsoft.Data.Odbc.OdbcConnection conn = new OdbcConnection("dsn=est_back;uid=dba;pwd=;pwd=dbserver");
OdbcCommand cmd = new OdbcCommand(sql, conn);
conn.Open();
OdbcDataReader dr = cmd.ExecuteReader();
if(dr.Read())
{
Response.Clear();
Response.AddHeader("Content- Type",dr"FileType"].ToString());
Response.BinaryWrite((byte[])dr["imgData"]);
Response.End();
}
dr.Close();
conn.Close();
}

其它都用你的方法,发现输出后竟为 showfile.aspx 原内容(如上)!但IE的菜单加入了
Word的菜单(说明已激活了Word),救救我!
gOODiDEA 2003-09-15
  • 打赏
  • 举报
回复
--》Response.AddHeader("Content-Type",dr["FileType"].ToString());
最好强行指定为Zip类型才会强迫IE下载
acewang 2003-09-15
  • 打赏
  • 举报
回复
在SQL Server中保存和输出任意类型的文件
我们可以把任意类型的文件保存到SQL Server中,在进行例子之前,先建立测试用表格,TestFile.sql:

if exists (select * from dbo.sysobjects where id = object_id(N'[dbo].[TestFiles]')
and OBJECTPROPERTY(id, N'IsUserTable') = 1)
drop table [dbo].[TestFiles]
GO

CREATE TABLE [dbo].[TestFiles] (
[id] [int] IDENTITY (1, 1) NOT NULL ,
[MyFileName] [varchar] (50) COLLATE Chinese_PRC_CI_AS NOT NULL ,
[FileType] [varchar] (50) COLLATE Chinese_PRC_CI_AS NOT NULL ,
[MyFile] [image] NOT NULL
) ON [PRIMARY] TEXTIMAGE_ON [PRIMARY]
GO

下面创建上传表单:

一旦提交了表单,我们使用HtmlInputFile类的PostedFile属性来访问我们上载的文件,用HttpPostedFile类的属性和方法来进行读取、保存上载文件和得到上载文件的其它信息。这里我们不使用SaveAs方法,因为它是用来保存文件的。我们要把数据保存到数据库中,我们使用InputStream属性,它用来初始化流来读取我们的数据。同时,我们使用ContentLength来读取文件大小,ContentType读取文件类型。然后创建byte数组,把文件流保存进该数组,然后保存到数据库即可。

下面就是完整的代码【CS版本】UploadFile.aspx:

<% @Page Language="C#" %>
<% @Import Namespace="System.IO" %>
<% @ Import Namespace="System.Data" %>
<% @ Import Namespace="System.Data.SqlClient" %>

<script runat="server">
public void UploadBtn_Click (Object sender, EventArgs e){

//得到提交的文件
Stream fileDataStream = MyFile.PostedFile.InputStream;

//得到文件大小
int fileLength = MyFile.PostedFile.ContentLength;

//创建数组
byte[] fileData = new byte[fileLength];

//把文件流填充到数组
fileDataStream.Read(fileData,0,fileLength);

//得到文件名字
string fileTitle = MyFileName.Value;

//得到文件类型
string fileType = MyFile.PostedFile.ContentType;

//构建数据库连接,SQL语句,创建参数
SqlConnection connection = new SqlConnection("Server=.;uid=sa;pwd=;Database=TestUploadFile");
SqlCommand command = new SqlCommand ("INSERT INTO TestFiles (MyFileName,MyFile,FileType)" +
"VALUES (@MyFileName,@MyFile,@FileType)", connection);

SqlParameter paramTitle = new SqlParameter ("@MyFileName", SqlDbType.VarChar,35);
paramTitle.Value = fileTitle;
command.Parameters.Add(paramTitle);

SqlParameter paramData = new SqlParameter ("@MyFile", SqlDbType.Image);
paramData.Value = fileData;
command.Parameters.Add(paramData);

SqlParameter paramType = new SqlParameter ("@FileType", SqlDbType.VarChar,25);
paramType.Value = fileType;
command.Parameters.Add(paramType);

//打开连接,执行查询
connection.Open();
command.ExecuteNonQuery();
connection.Close();

Message.Text="你的文件已经成功上载";
MyFileName.Value = "";
}
</script>
<hr>
<asp:label id="Message" Text="选择文件和文件名字:" runat="server"/>
<hr>
<form method="post" enctype="multipart/form-data" runat="server">
<b>文件名字:</b><input id="MyFileName" type="text" runat="server">
<P>
<b>文件:</b><input id="MyFile" type="file" runat="server">
<br><br>
<input type=submit value="开始上传" OnServerclick="UploadBtn_Click" runat="server">
</form>

一旦我们上载成功,我们可以对文件进行浏览:只需要设置页面的MIME类型,然后用Response对象的BinaryWrite()进行输出。

ShowUploadFile.aspx

<% @Page Language="C#" %>
<% @Import Namespace="System.IO" %>
<% @ Import Namespace="System.Data" %>
<% @ Import Namespace="System.Data.SqlClient" %>
<script runat="server">
private void Page_Load(Object sender, EventArgs e) {
string sql="SELECT * FROM TestFiles";
SqlConnection connection = new SqlConnection("Server=.;uid=sa;pwd=;Database=TestUploadFile");
SqlCommand command = new SqlCommand(sql, connection);
connection.Open();
FileList.DataSource = command.ExecuteReader();
FileList.DataBind();
connection.Close();
}
</script>

<form runat="server">
<asp:DataGrid id="FileList" runat="server"
BorderColor="orange" BorderWidth="2" CellPadding="4"
AutoGenerateColumns="false" ShowHeader="true" Align="center">
<HeaderStyle BorderColor="White" BackColor="black" ForeColor="White"
Font-Bold="True" Font-Size="9" HorizontalAlign="Center"/>
<Columns>
<asp:TemplateColumn HeaderText="文件名字">
<ItemTemplate>
<b>
<%# DataBinder.Eval(Container.DataItem, "MyFileName") %>
</b>
</ItemTemplate>
</asp:TemplateColumn>
<asp:TemplateColumn HeaderText="类型">
<ItemTemplate>
<b>
<%# DataBinder.Eval(Container.DataItem, "FileType") %>
</b>
</ItemTemplate>
</asp:TemplateColumn>

<asp:TemplateColumn HeaderText="查看">
<ItemTemplate>
<b>
<a href="ShowFile.aspx?ID=<%# DataBinder.Eval(Container.DataItem, "ID") %>">查看文件</a>
</b>
</ItemTemplate>
</asp:TemplateColumn>
</Columns>
</asp:DataGrid>
</form>

ShowFile.aspx

<% @Page Language="C#" %>
<% @Import Namespace="System.IO" %>
<% @ Import Namespace="System.Data" %>
<% @ Import Namespace="System.Data.SqlClient" %>
<script runat="server">
private void Page_Load(Object sender, EventArgs e) {
string sql="SELECT * FROM TestFiles WHERE ID = '" + Request.QueryString["ID"] + "'";
SqlConnection connection = new SqlConnection("Server=.;uid=sa;pwd=;Database=TestUploadFile");
SqlCommand command = new SqlCommand(sql, connection);
connection.Open();
SqlDataReader dr = command.ExecuteReader();
if(dr.Read()){
Response.Clear();
Response.AddHeader("Content-Type",dr["FileType"].ToString());
Response.BinaryWrite((byte[])dr["MyFile"]);
}
dr.Close();
connection.Close();
}
</script>

losting 2003-09-15
  • 打赏
  • 举报
回复
谢谢快乐王子 ,我试用过你的方法,你是指将<%@Page language="C#"%>以下整个文件存为showfile.aspx吗?我这样用,但因我用ODBC,不用使用<%@import namespace="Microsoft.Data.Odbc"%>(说我没有引用),改为代码后置变为:

showfile.aspx 的内容:
<%@ Page language="c#" Codebehind="showfile.aspx.cs AutoEventWireup="false" Inherits="WordApplication.showfile" %>

showfile.aspx.cs 的内容:
using System;
using System.Collections;
using System.ComponentModel;
using System.Data;
using System.Drawing;
using System.Web;
using System.Web.SessionState;
using System.Web.UI;
using System.Web.UI.WebControls;
using System.Web.UI.HtmlControls;
using Microsoft.Data.Odbc;
namespace WordApplication
{
/// <summary>
/// Summary description for showfile.
/// </summary>
public class showfile : System.Web.UI.Page
{
private void Page_Load(object sender, System.EventArgs e)
{
string strFileName="myouttest.doc";
string sql="SELECT fileid, filetype,imgSize,imgData FROM t_imgs WHERE fileid = 'testid2'";
Microsoft.Data.Odbc.OdbcConnection conn = new OdbcConnection("dsn=est_back;uid=dba;pwd=;pwd=dbserver");
OdbcCommand cmd = new OdbcCommand(sql, conn);
conn.Open();
OdbcDataReader dr = cmd.ExecuteReader();
if(dr.Read())
{
Response.ContentType = (string)dr["filetype"];
Response.OutputStream.Write((byte[])dr["imgData"], 0,(int)dr["imgSize"]);
Response.End();
}
dr.Close();
conn.Close();

}


但发现Word输出为空的,PDF输出为ASCII文件!加上Response.ContentType="application/octet-stream";
Response.AddHeader("Content-Disposition", "attachment;filename=" & strFileName);
后,Word文件附件下载为XML原码(且是乱码),但PDF文件附件下载正常。请指教,我该如何处理?不胜感激!


pengcheng 2003-09-15
  • 打赏
  • 举报
回复
up
panyee 2003-09-14
  • 打赏
  • 举报
回复
输出应该跟输出图片一样,如果是要保存的话,就加上两句:

Response.ContentType = "application/octet-stream";
Response.AddHeader("Content-Disposition", "attachment;filename=" & strFileName);


<%@Page language="C#"%>
<%@import namespace="System.Data"%>
<%@import namespace="System.Data.SqlClient"%>
<script language="C#" runat="server">
public void Page_Load(Object o, EventArgs e)
{
int ImgID = Convert.ToInt32(Request.Params["id"]);
string connStr = ConfigurationSettings.AppSettings["ConnectionString"];
SqlConnection conn = new SqlConnection(connStr);
string sql = "select * from t_imgs where id = @ImgID";
SqlCommand cmd = new SqlCommand(sql, conn);
cmd.Parameters.Add("@ImgID", SqlDbType.Int).Value = ImgID;
conn.Open();
SqlDataReader read = cmd.ExecuteReader();
read.Read();
Response.ContentType = (string)read["type"];
Response.OutputStream.Write((byte[])read["imgData"], 0, (int)read["imgSize"]);
Response.End();
conn.Close();
}
</script>
cnlamar 2003-09-14
  • 打赏
  • 举报
回复
用StreamWriter试试?

62,025

社区成员

发帖
与我相关
我的任务
社区描述
.NET技术交流专区
javascript云原生 企业社区
社区管理员
  • ASP.NET
  • .Net开发者社区
  • R小R
加入社区
  • 近7日
  • 近30日
  • 至今
社区公告

.NET 社区是一个围绕开源 .NET 的开放、热情、创新、包容的技术社区。社区致力于为广大 .NET 爱好者提供一个良好的知识共享、协同互助的 .NET 技术交流环境。我们尊重不同意见,支持健康理性的辩论和互动,反对歧视和攻击。

希望和大家一起共同营造一个活跃、友好的社区氛围。

试试用AI创作助手写篇文章吧