上传文件时如何把文件直接存入数据库?word文件

numbersoft 2002-09-19 09:58:36
none
...全文
458 16 打赏 收藏 转发到动态 举报
写回复
用AI写文章
16 条回复
切换为时间正序
请发表友善的回复…
发表回复
xiaomaoxia 2002-10-13
  • 打赏
  • 举报
回复
谢谢~~~~~~~~~~~~~~~~~~~~~
numbersoft 2002-10-11
  • 打赏
  • 举报
回复
给你一套代码有,存数据,读数据,存数据到服务器目录下等(数据为Word)。
private void Button1_Click(object sender, System.EventArgs e)//直接存到数据库
{
if(SearchFile.PostedFile!=null)
{
/*string FileName;
FileName=Path.GetFileName(SearchFile.PostedFile.FileName);
str=Path.Combine(Path.Combine(Request.MapPath(Request.ApplicationPath),"File\\"),FileName);
Label1.Text=str;
SearchFile.PostedFile.SaveAs(str);
Page.RegisterStartupScript("openwindow","<script>window.open('File/"+Server.UrlDecode(FileName)+"')</script>");
*/
string FileName;
Stream WordStream=SearchFile.PostedFile.InputStream;
string FilePath=this.SearchFile.PostedFile.FileName;
FileName=Path.GetFileName(FilePath);
int WordLen=SearchFile.PostedFile.ContentLength;
string WordType=SearchFile.PostedFile.ContentType;
byte[] WordData=new Byte[WordLen];
int n=WordStream.Read(WordData,0,WordLen);
WordStream.Close();
scom=new System.Data.SqlClient.SqlCommand();
this.scom.CommandText="insert into MyTable(name,FileBinary) values(@FileName,@FileBinary)";
this.scom.Parameters.Add(new System.Data.SqlClient.SqlParameter("@FileName",System.Data.SqlDbType.Char,20,"FileName"));
this.scom.Parameters.Add(new System.Data.SqlClient.SqlParameter("@FileBinary",System.Data.SqlDbType.Image,WordData.Length,"FileBinary"));
this.scom.Connection=this.sqlConnection;
this.scom.Parameters["@FileName"].Value=FileName;
this.scom.Parameters["@FileBinary"].Value=WordData;
this.scom.Connection.Open();
scom.ExecuteNonQuery();
this.scom.Connection.Close();

}
}

private void Button2_Click(object sender, System.EventArgs e)//通过数据流的方式在浏览器中显示Word文件
{
//Response.AddHeader("Content-Disposition", "attachment; filename=" + "nihao.html");

Response.ContentType="Application/msword";

this.Response.Clear();

this.selcom=new System.Data.SqlClient.SqlCommand();
this.selcom.CommandText="select name,FileBinary from MyTable";
this.selcom.Connection=this.sqlConnection;
this.selcom.Connection.Open();
SqlDataReader dr=this.selcom.ExecuteReader();
dr.Read();
Byte[] b=new Byte[(dr.GetBytes(1,0,null,0,int.MaxValue))];
dr.GetBytes(1,0,b,0,b.Length);
dr.Close();
this.selcom.Connection.Close();
System.IO.Stream fs=this.Response.OutputStream;
fs.Write(b,0,b.Length);

fs.Close();
this.Response.End();
}

private void Button3_Click(object sender, System.EventArgs e)//据数据存到服务器并命名再显示。
{
this.selcom2=new System.Data.SqlClient.SqlCommand();
this.selcom2.CommandText="select name,FileBinary from MyTable";
this.selcom2.Connection=this.sqlConnection;
this.selcom2.Connection.Open();
SqlDataReader dr=this.selcom2.ExecuteReader();
dr.Read();
string FileName=dr["name"].ToString();
Byte[] b=new Byte[(dr.GetBytes(1,0,null,0,int.MaxValue))];
dr.GetBytes(1,0,b,0,b.Length);
dr.Close();
this.selcom2.Connection.Close();
Request.MapPath(Request.ApplicationPath);
string wuliPath=Path.Combine(Path.Combine(Request.MapPath(Request.ApplicationPath),"File\\"),FileName);
System.IO.Stream fs=new FileStream(wuliPath, FileMode.Create, FileAccess.Write);
fs.Write(b,0,b.Length);
fs.Close();
Page.RegisterStartupScript("openwindow","<script>window.open('File/"+Server.UrlDecode(FileName)+"')</script>");
}

----------------------------------------------------------------
原贴内容:
你好, 刚才看了你的帖子:上传文件时如何把文件直接存入数据库?word文件
不知你的问题解决没有? 还有就是客户端如何从页面把读出上传的写入服务端数据库的文件, 并在客户端调用相应的程序打开, 如存储在数据库中的是word文档则用word打开, 我学asp.net不久, 很多问题没解决, 正在做上传文件的程序, 苦于毫无经验啊, 谢谢帮忙, 给我点代码最好了,^_^
webmasterforyou@21cn.com
Runny 2002-09-28
  • 打赏
  • 举报
回复
强调一下:
我想实现的是:一点对多点(下发)及一对一(上传);在一个间隔上千公里的专网内部实现文件的异地传输。 我用filecopy函数,但在win2000下存在身份验证的麻烦,用saveas也是相同问题。在asp.net中可否调用ftp来实现我上面的问题,若可以,该怎么操作?同样,请给位大虾帮帮忙忙,若代码为vb.net的最好。
Runny 2002-09-28
  • 打赏
  • 举报
回复
我也借宝地向各位请教个问题:
在asp.net中怎样进行文件的异地上传、下发(一点对多点)? 我用filecopy函数,但在win2000存在下存在身份验证的麻烦,用saveas也是相同问题。请给位大虾帮帮忙忙,若代码位vb.net的最好。
numbersoft 2002-09-23
  • 打赏
  • 举报
回复
楼上的楼上你把
Response.clear()和Response.End()拿掉试试。
ColorSM 2002-09-23
  • 打赏
  • 举报
回复
qq
visual 2002-09-23
  • 打赏
  • 举报
回复
to LostAngel911:
可是下载呢?对于图片来说读出来没问题,可是怎样能让一些文件下载呢?请大家有时间看看上面我的帖子.
谢谢.
LostAngel911 2002-09-21
  • 打赏
  • 举报
回复
http://www.csdn.net/expert/topic/984/984400.xml?temp=.8761408
不仅仅可以上传图片
任何文件都可以上传
visual 2002-09-21
  • 打赏
  • 举报
回复
借宝地问微软专家。
----------------------------
我的附件(任何文件)存在数据库的image字段内,怎样让其下载时不出现那个多余的窗口?
string l_AttachID=Request.QueryString["Key"];

string connstr=ConfigurationSettings.AppSettings["ConnString"];
SqlConnection connection = new SqlConnection(connstr);
SqlCommand command;
string strSql="";
SqlDataReader dr;
connection.Open();

strSql="SELECT * from MsgAttach where ID="+l_AttachID;
command= new SqlCommand(strSql, connection );
dr=command.ExecuteReader();
string l_FileName="";
int l_Size=0;
byte[] sContent;
if (dr.Read())
{

l_FileName=dr["附件名称"].ToString();
l_Size=int.Parse(dr["附件大小"].ToString());
sContent=(byte[])dr["附件"];
Response.AppendHeader("Content-Disposition", "attachment; filename="+HttpUtility.UrlEncode(l_FileName,System.Text.Encoding.UTF8));
Response.BinaryWrite(sContent);
Response.End();

}
dr.Close();
connection.Close();
//////////////////////////////////////////////////////
上传很正常,下载基本正常,但是每次都会先弹出这个aspx的空白页面,然后出现提示是否下载的对话框,即使下载完后这个空白页面也不消失。

问题在: http://www.csdn.net/Expert/TopicView2.asp?id=877453&datebasetype=now

谢谢贴主和微软专家
zgh_ms 2002-09-20
  • 打赏
  • 举报
回复
感谢您使用微软产品。

在上传的时候,我们可以使用<input type="file" runat=server...> HTML ServerControl, 然后在服务器端得到对应的文件流,把文件流写入数组,然后倒入数据库。

下面是我写的一个示例程序:File1为HTML File ServerControl对象:

注意:假设数据库为test_00, 表为imageFile, 内有两个子段,fileName和fileData.

////////////////////////////////////////////////////////////////////////////


if( ( File1.PostedFile != null ) && ( File1.PostedFile.ContentLength > 0 ) )
{
string SourceFilePath=this.File1.PostedFile.FileName;
string DBcon=@"server=localhost;integrated security=yes;database=test_00";
System.IO.Stream fs=this.File1.PostedFile.InputStream;

bool b=Handler.FileToSqlBlob(SourceFilePath,DBcon,fs);
Response.Write(b.ToString());
}

else
{
Response.Write("Please select a file to upload.");
}

下面这个函数,接受三个参数,文件名,数据库连接串,流对象, 然后把流读入数组,计入数据库。

////////////////////////////////////////////////////////////////////////////

public static bool FileToSqlBlob(string SourceFilePath,string ConString,System.IO.Stream fs)
{
string fileName="";
bool bresult=true;
try
{
fileName=System.IO.Path.GetFileName(SourceFilePath);
SqlConnection cn=new SqlConnection(ConString);
SqlCommand cmd=new SqlCommand(@"Insert into imagefile(filename,filedata) values( @FileName,@FileData)",cn);


Byte[] b=new Byte[fs.Length];
fs.Read(b,0,b.Length);
fs.Close();

SqlParameter pFileName=new SqlParameter("@FileName",SqlDbType.NVarChar,fileName.Length,ParameterDirection.Input,false,0,0,null,DataRowVersion.Current,fileName);

SqlParameter pFileData=new SqlParameter("@FileData",SqlDbType.VarBinary, b.Length, ParameterDirection.Input, false, 0, 0, null, DataRowVersion.Current, b);

cmd.Parameters.Add(pFileName);
cmd.Parameters.Add(pFileData);

cn.Open();
cmd.ExecuteNonQuery();
cn.Close();
}

catch(SqlException ex)
{
bresult=false;
}
return bresult;
}

////////////////////////////////////////////////////////////////////////////

同时,我们可以通过下面的代码,把数据库中的文件读出,通过Response.OutputStream,发送到浏览器端:

////////////////////////////////////////////////////////////////////////////
string DBcon=@"server=localhost;integrated security=yes;database=test_00";

// Response.ContentType="Application/x-msexcel";

Response.ContentType="Application/msword";

this.Response.Clear();
Handler.SqlBlobToFile(Session["file"].ToString(),DBcon,this.Response.OutputStream);
this.Response.End();


下面是SqlBlobToFile函数的实现:
////////////////////////////////////////////////////////////////////////////
public static bool SqlBlobToFile(string fileName,string ConString, System.IO.Stream fs)
{

bool bresult=true;

try
{
int fileDataCol = 0;
SqlConnection cn=new SqlConnection(ConString);
SqlCommand cmd=new SqlCommand(@"SELECT fileData FROM imagefile WHERE filename='"+fileName +@"'",cn);

cn.Open();

SqlDataReader dr=cmd.ExecuteReader();
dr.Read();

Byte[] b = new Byte[(dr.GetBytes(fileDataCol, 0, null, 0, int.MaxValue))];
dr.GetBytes(fileDataCol, 0, b, 0, b.Length);
dr.Close();

cn.Close();

fs.Write(b,0,b.Length);
fs.Close();
}

catch(SqlException ex)
{
bresult=false;
}

return bresult;

}

////////////////////////////////////////////////////////////////////////////

希望对您有所帮助。

-微软全球技术中心 -zgh

本贴子以“现状”提供且没有任何担保,同时也没有授予任何权利。具体事项可参见使用条款(http://support.microsoft.com/directory/worldwide/zh-cn/community/terms_chs.asp)。
为了为您创建更好的讨论环境,请参加我们的用户满意度调查(http://support.microsoft.com/directory/worldwide/zh-cn/community/survey.asp?key=(S,49854782))。
numbersoft 2002-09-20
  • 打赏
  • 举报
回复
问题解决了,是写入问题。thank's
zgh_ms 2002-09-20
  • 打赏
  • 举报
回复
谢谢回复。

Handler是我定义的类,把SqlBlobToFile和FileToSqlBlob放进去。

-微软全球技术中心 -zgh

本贴子以“现状”提供且没有任何担保,同时也没有授予任何权利。具体事项可参见使用条款(http://support.microsoft.com/directory/worldwide/zh-cn/community/terms_chs.asp)。
为了为您创建更好的讨论环境,请参加我们的用户满意度调查(http://support.microsoft.com/directory/worldwide/zh-cn/community/survey.asp?key=(S,49854782))。


numbersoft 2002-09-20
  • 打赏
  • 举报
回复
Handler这个是什么?
e:\inetpub\wwwroot\WebWord\WebForm1.aspx.cs(101): 找不到类型或命名空间名称“Handler”(是否缺少 using 指令或程序集引用?)
zgh_ms 2002-09-20
  • 打赏
  • 举报
回复
感谢您的回复。

建议您在读出函数里面设置几个断点,然后进行调试。

请您使用QuickWatch, 确保传入的文件名和其它变量不为空。

根据我的经验,如果文件名为空,则数据库查询会返回空, 所以导致这种错误。

-微软全球技术中心 -zgh

本贴子以“现状”提供且没有任何担保,同时也没有授予任何权利。具体事项可参见使用条款(http://support.microsoft.com/directory/worldwide/zh-cn/community/terms_chs.asp)。
为了为您创建更好的讨论环境,请参加我们的用户满意度调查(http://support.microsoft.com/directory/worldwide/zh-cn/community/survey.asp?key=(S,49854782))。
numbersoft 2002-09-20
  • 打赏
  • 举报
回复
Byte[] b=new Byte[(dr.GetBytes(fileDataCol,0,null,0,int.MaxValue))];
在这行中产生错误:数据为空。不能对空值调用此方法或属性。

我怎么没看到
dr["fileData"]代码?如何读出数据?
donwong 2002-09-20
  • 打赏
  • 举报
回复
gz

62,046

社区成员

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

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

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

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