sDocFileName = "c:\" + strUserID + "Temp" + CStr(iLoop) + ".doc" Dim FileSys As New FileStream(sDocFileName, FileMode.OpenOrCreate) Dim ByteWrite As New BinaryWriter(FileSys) ByteWrite.Write(dsResult.Rows(iLoop).Item("文件内容")) FileSys.Close() ByteWrite.Close() 请试试看这段代码,另外记得加入Imports System,
我要做的是向浏览器做输出! 在浏览器上形成一个word文件!
怎么没人给我回答啊! to: ZHANG9652(剑神独孤求败) ( ) 我现在是要向浏览器输出word文件,就是让浏览器出现选择保存还是打开的对话框。并且打开的时候保证word文件不是乱码,现在我在浏览器打开的时候内容显示是乱码。
Dim con As SqlClient.SqlConnection Dim SqlCmd = "SELECT * from CustomerAffix where ID='" & Request.QueryString("ID") & "'" con = New SqlClient.SqlConnection(Context.GetConfig("appSettings")("dsn")) con.Open() Dim ada As New SqlClient.SqlDataAdapter(SqlCmd, con) Dim ds As New DataSet() ada.Fill(ds, "wendang") Dim FileByteArray() As Byte = ds.Tables(0).Rows(0).Item("Affix") Dim s = ds.Tables(0).Rows(0).Item("Affix") s = FileByteArray.Length Response.ContentType = ds.Tables(0).Rows(0).Item("Type") Response.AddHeader("Content-Disposition ", "attachment; filename= " & ds.Tables(0).Rows(0)("AffixName").ToString()) Response.OutputStream.Write(ds.Tables(0).Rows(0).Item("Affix"), 0, CInt(s)) Page.Response.Flush() Page.Response.End() con.Close()
我用的ado,我改成了这样: objChaXunRs是一个返回的recordset集合: Dim FileByteArray() As Byte = objChaXunRs("File_Comment").Value Dim s As Long s = FileByteArray.Length Response.Charset = "gb2312" Response.ContentType = "application/msword" Response.AppendHeader("Content-Disposition:", "attachment; filename=" & HttpUtility.UrlEncode("正文.doc")) Response.OutputStream.Write(objChaXunRs("File_Comment").Value, 0, CInt(s)) Response.Flush() Response.End() 但是现在还是不行,不知道怎么了!
string SqlStr = "select * from filetest"; SqlDataConnector con = new SqlDataConnector(); con.SqlOpen(); SqlDataReader dr = con.GetDataReader(SqlStr); if(dr.Read()) {
Dim SqlStr = "select * from filetest" Dim com As SqlClient.SqlCommand = New SqlClient.SqlCommand() Dim con As SqlClient.SqlConnection = New SqlClient.SqlConnection() con.ConnectionString = "data source=zhuyuanbing;initial catalog=OA;persist security info=true;user id=sa;packet size=4096" con.Open() com.CommandText = SqlStr ' dim dr as SqlClient.SqlDataReader = com.Connection = con Dim dr As SqlClient.SqlDataReader = com.ExecuteReader() If (dr.Read()) Then Response.ContentType = Convert.ToString(dr("fileType")) Response.AddHeader("Content-Disposition:", "attachment; filename=" + HttpUtility.UrlEncode("正文.doc")) Dim file() As Byte = dr("fileDetail") Response.BinaryWrite(file) Response.Flush() End If这是Vb的代码,我也测试过了
不太可能吧?你用的数据库中保存文件的字段是什么类型的?我用的是image类型的,使用的是Sql server 2000。还有,我全都用的是.net,没有使用其它的技术,用的Ado.net。
Dim FileSys As New FileStream(sDocFileName, FileMode.OpenOrCreate)
Dim ByteWrite As New BinaryWriter(FileSys)
ByteWrite.Write(dsResult.Rows(iLoop).Item("文件内容"))
FileSys.Close()
ByteWrite.Close()
请试试看这段代码,另外记得加入Imports System,
在浏览器上形成一个word文件!
to: ZHANG9652(剑神独孤求败) ( )
我现在是要向浏览器输出word文件,就是让浏览器出现选择保存还是打开的对话框。并且打开的时候保证word文件不是乱码,现在我在浏览器打开的时候内容显示是乱码。
Dim SqlCmd = "SELECT * from CustomerAffix where ID='" & Request.QueryString("ID") & "'"
con = New SqlClient.SqlConnection(Context.GetConfig("appSettings")("dsn"))
con.Open()
Dim ada As New SqlClient.SqlDataAdapter(SqlCmd, con)
Dim ds As New DataSet()
ada.Fill(ds, "wendang")
Dim FileByteArray() As Byte = ds.Tables(0).Rows(0).Item("Affix")
Dim s = ds.Tables(0).Rows(0).Item("Affix")
s = FileByteArray.Length
Response.ContentType = ds.Tables(0).Rows(0).Item("Type")
Response.AddHeader("Content-Disposition ", "attachment; filename= " & ds.Tables(0).Rows(0)("AffixName").ToString())
Response.OutputStream.Write(ds.Tables(0).Rows(0).Item("Affix"), 0, CInt(s))
Page.Response.Flush()
Page.Response.End()
con.Close()
objChaXunRs是一个返回的recordset集合:
Dim FileByteArray() As Byte = objChaXunRs("File_Comment").Value
Dim s As Long
s = FileByteArray.Length
Response.Charset = "gb2312"
Response.ContentType = "application/msword"
Response.AppendHeader("Content-Disposition:", "attachment; filename=" & HttpUtility.UrlEncode("正文.doc"))
Response.OutputStream.Write(objChaXunRs("File_Comment").Value, 0, CInt(s))
Response.Flush()
Response.End()
但是现在还是不行,不知道怎么了!
SqlDataConnector con = new SqlDataConnector();
con.SqlOpen();
SqlDataReader dr = con.GetDataReader(SqlStr);
if(dr.Read())
{
this.Response.ContentType = Convert.ToString(dr["fileType"]);
Response.AddHeader("Content-Disposition:", "attachment; filename=" + HttpUtility.UrlEncode("正文.doc"));
object dbValue = dr["fileDetail"];
byte[] file = (byte[])dbValue;
this.Response.BinaryWrite(file);
this.Response.Flush(); }
dr.Close();其中,SqlDataConnector是我自己写的一个连接数据库的类了,不是系统的。你可以自己写了。我数据库里面的dr["fileType"]读出来就是你那个传输类型了。因为我是上传上去的,所以给了这样一个东西,为了通用而已。关键在最后,用字节数组和二进制书写器。c#的代码,我已经试过了,没有问题了!你对着翻译成VB的代码就可以了
可以直接打开文件在WORD里面
顺便给你上传的代码,也是和别人学的,大家一起学习。
byte[] FileContent = new Byte[this.File1.PostedFile.InputStream.Length];
this.File1.PostedFile.InputStream.Position =0;
this.File1.PostedFile.InputStream.Read(FileContent,0,FileContent.Length);
string fileType = this.File1.PostedFile.ContentType;
string fileName = this.File1.PostedFile.FileName;
string SqlStr = "insert filetest(fileName,fileType,fileDetail) values(@Name,@fileType,@fileDetail)";
SqlCommand com = new SqlCommand(SqlStr);
com.Parameters.Add("@Name",fileName);
com.Parameters.Add("@fileType",fileType);
com.Parameters.Add("@fileDetail",FileContent);
SqlDataConnector con = new SqlDataConnector();
con.SqlOpen();
con.ExecuteSqlCommand(com);
Dim com As SqlClient.SqlCommand = New SqlClient.SqlCommand()
Dim con As SqlClient.SqlConnection = New SqlClient.SqlConnection()
con.ConnectionString = "data source=zhuyuanbing;initial catalog=OA;persist security info=true;user id=sa;packet size=4096"
con.Open()
com.CommandText = SqlStr
' dim dr as SqlClient.SqlDataReader =
com.Connection = con
Dim dr As SqlClient.SqlDataReader = com.ExecuteReader()
If (dr.Read()) Then
Response.ContentType = Convert.ToString(dr("fileType"))
Response.AddHeader("Content-Disposition:", "attachment; filename=" + HttpUtility.UrlEncode("正文.doc"))
Dim file() As Byte = dr("fileDetail")
Response.BinaryWrite(file)
Response.Flush() End If这是Vb的代码,我也测试过了
这是aspx的html代码的一部分,你看你的类型是不是这样的。还有,你新建一个webform用我给你的上传下载代码看看。你看看你以前上传的时候是不是文件本身就在传输时有问题。还有了,我的上传用的时.net中的那个上传组件,另外我用的是中文版的。这些你都看看吧!
楼主注意,doc文档中除了数据本身,还有许多控制信息。不信你把一个txt文件保存为doc再打开试试,也是乱码!