当前位置:编程学习 > C#/ASP.NET >>

在ASP.Net网站中上传下载Word文件问题!

我把一个Word文档上传到网站下的File文件夹中[数据库中存储了这个Word的名称 Name(名称就是文件的真实名称),存储路径FilePath],现在是想实现从File文件夹里下载这个文档到当地。怎么写?现在下载代码不会写,求源码,请高手赐教! --------------------编程问答-------------------- 前台aspx页面放置一个inputfile表单元素 注意放置HTML的控件

#region 上传文件到数据库和服务器
public void FN_UpFiles()
{
//遍历File表单元素
HttpFileCollection files = HttpContext.Current.Request.Files;
StringBuilder strMsg = new StringBuilder();
strMsg.Append("上传的文件分别是:<hr color='pink'/>");
try
{
for( int iFile = 0 ; iFile < files.Count ; iFile++ )
{
//检查文件扩展名字
HttpPostedFile postedFile = files[iFile];
string fileName = "";
string fileExtension = "";
fileName = Path.GetFileName(postedFile.FileName);
if( fileName != "" )
{
try
{
string strpath = HttpContext.Current.Server.MapPath("/") + fileName;
if( System.IO.File.Exists(strpath) )
{
Response.Write("文件已经存在!" + fileName);
NR.Error.Log.LogType("文件" + fileName + "已经存在");
}
else
{
try
{
NRModel.File model = new NRModel.File();
NRBLL.File bf = new NRBLL.File();
Guid guid1 = Guid.NewGuid();
Guid guid2 = Guid.NewGuid();
Guid guid3 = Guid.NewGuid();
Guid guid4 = Guid.NewGuid();
model.Fileid = guid1;
model.Folderid = guid2;
model.Filepath = strpath;
model.FileNam = fileName.ToString();
model.FileSize = postedFile.ContentLength;
model.Decription = TextArea1.InnerText.ToString();
model.CreateOn = DateTime.Now;
model.CreateBy = guid3;
model.ModefyBy = guid4;
if( bf.FN_AddNewRes(model) > 0 )
{
NR.Error.Log.LogType("上传资源" + fileName + "成功!");
fileExtension = Path.GetExtension(fileName);
strMsg.Append("上传的文件类型:" + postedFile.ContentType.ToString() + "<br>");
strMsg.Append("客户端文件名称:" + postedFile.FileName + "<br>");
strMsg.Append("上传文件的文件名:" + fileName + "<br>");
strMsg.Append("上传文件的扩展名:" + fileExtension + "<br>");
strMsg.Append("上传文件的大小:" + postedFile.ContentLength.ToString() + "个字节" + "<br>");
postedFile.SaveAs(System.Web.HttpContext.Current.Server.MapPath("/") + fileName);

Page.RegisterStartupScript("提示","<script language='javascript'>alert('上传成功!');self.opener.location.reload();window.close();</script>");
//Response.Write("<script language='javascript'>self.opener.location.reload();</script>");
//Response.Write("<script language='javascript'>window.close();</script>");
}
}
catch( Exception ex )
{
NR.Error.Log.LogType(ex.ToString());
}
}
}
catch( Exception ex )
{
NR.Error.Log.LogType(ex.ToString());
}
}
else
{
Response.Write("上传文件不能为空!");
NR.Error.Log.LogType("文件不能为空!");
}
}
strStatus.Text = strMsg.ToString();
}
catch( System.Exception ex )
{
NR.Error.Log.LogType(ex.ToString());
}
}
#endregion
--------------------编程问答-------------------- 简单点,直接用A标签指向该文件便可 --------------------编程问答-------------------- 。。。看错 下载啊  

string id = context.Request["id"].ToString();//获取资源的编号
System.IO.Stream iStream = null;
byte[] buffer = new Byte[10000];
int length;
long dataToRead;
NRBLL.File bf = new Asiastar.NRBLL.File();
Guid guid = new Guid(id);
if( bf.FN_SerchPathByFileId(guid).Tables[0].Rows[0]["FilePath"] != null )//判断数据库路径是否存在
{
string filepath = bf.FN_SerchPathByFileId(guid).Tables[0].Rows[0]["FilePath"].ToString();//获取资源完整路径
string filename = System.IO.Path.GetFileName(filepath);//获取资源名称

try
{
string fileName = HttpUtility.UrlEncode(System.Text.Encoding.UTF8.GetBytes(filename));//解码(注意这里2层解码)
filename = filename.Replace("+","%20");  //将“+”替换成“空格”
iStream = new System.IO.FileStream(filepath,System.IO.FileMode.Open,System.IO.FileAccess.Read,System.IO.FileShare.Read);
dataToRead = iStream.Length;
context.Response.ContentType = "application/octet-stream";
context.Response.AddHeader("Content-Disposition","attachment; filename=" + HttpUtility.UrlEncode(filename,System.Text.Encoding.UTF8));
while( dataToRead > 0 )
{
if( context.Response.IsClientConnected )
{
length = iStream.Read(buffer,0,10000);
context.Response.OutputStream.Write(buffer,0,length);
context.Response.Flush();
buffer = new Byte[10000];
dataToRead = dataToRead - length;
}
else
{
dataToRead = -1;
}
}

}
catch( Exception ex )
{
NR.Error.Log.LogType(ex.ToString());
}
finally
{
if( iStream != null )
{
iStream.Close();
}
}
}
else
{
NR.Error.Log.LogType("找不到文件!");
}
--------------------编程问答-------------------- 谢谢一楼,我主要是想要下载的代码。就是想遍历File文件夹下的文件,如果文件名等于数据库中的文件名(Name)就下载! --------------------编程问答-------------------- 谢谢了,我写一下吧! --------------------编程问答-------------------- 直接链接文件名就可以下载了,例如:http://topic.csdn.net/File/1.doc --------------------编程问答--------------------
引用 4 楼 xiujuan584868710 的回复:
谢谢一楼,我主要是想要下载的代码。就是想遍历File文件夹下的文件,如果文件名等于数据库中的文件名(Name)就下载!

你应该再加一个字段来存放文件的完整路径 .建议
补充:.NET技术 ,  ASP.NET
CopyRight © 2012 站长网 编程知识问答 www.zzzyk.com All Rights Reserved
部份技术文章来自网络,