如何下载文件夹内的word文件,文件名称存放在数据库中

yuantaolzu 2008-07-15 12:56:17
各位大侠,小弟碰到问题如下:
我将word文件上传到upload文件夹内,同时将文件名称保存到一个TEST表内。表内有字段ID和FILE,类型分别为int和vchar类型。然后通过gridview数据控件浏览数据库内的记录。
小弟想实现功能如下:在gridview内的相应字段上点击即可弹出下载对话框直接下载文件。小弟知道需通过ID字段重定向到相对应文件夹,但具体如何实现却一直实现不了。希望哪位大哥给一段源码,小弟菜鸟,还望各位大哥能不吝赐教!
如能帮忙解决问题,100分为谢!
...全文
155 7 打赏 收藏 转发到动态 举报
写回复
用AI写文章
7 条回复
切换为时间正序
请发表友善的回复…
发表回复
yuantaolzu 2008-07-15
  • 打赏
  • 举报
回复
非常感谢各位,可能我的问题说的不是太清除,已经按照walkghost的方法解决,虽然没有用到fly和fellowcheng的方法,但同样感谢二位的热情帮助。若我的分够100分的话会马上结贴,分别赠送walkghost,fly ,fellowcheng的分数为60分,20分,20分。若本人分数不够,则会在最近几天努力挣分,争取3天内结贴。
再次对各位表示感谢!
sxmonsy 2008-07-15
  • 打赏
  • 举报
回复
友情UP+接分
walkghost 2008-07-15
  • 打赏
  • 举报
回复
要用到一个超链接列:
<asp:HyperLinkField DataNavigateUrlFields="url" DataNavigateUrlFormatString="~/play.aspx?id={0}"
Target="_blank" Text="试听" />
你把url改成file,把~/play.aspx?id={0}改成~/upload/{0}。试听改成下载。
当你点下载的时候,URL就变成了“你的域名/upload/数据库中file字段的值”。
我以前做一个音乐网站的时候就这样用的。我的url保存的是音乐的地址。
kbryant 2008-07-15
  • 打赏
  • 举报
回复
关注帮顶
fellowcheng 2008-07-15
  • 打赏
  • 举报
回复

protected void DownFile(int fileID, string strType)
{
string strFileFullName = null;

FileInfo file = new FileInfo(strFileFullName);
if (file.Exists)
{
string strContentType = GetContentType(file.Extension.ToLower());

Response.Clear();
Response.ClearHeaders();
Response.ClearContent();
Response.ContentType = strContentType;
Response.AddHeader("Content-disposition", "attachment; filename=" + HttpUtility.UrlEncode(file.Name));
Response.WriteFile(strFileFullName);
Response.Flush();
Response.End();
}
}

/// <summary>
/// 根据文件扩展名返回ContentType
/// </summary>
/// <param name="strFileExtension">文件扩展名</param>
/// <returns></returns>
private string GetContentType(string strFileExtension)
{
string strType = "application/octet-stream";
if (strFileExtension.Length > 1)
{
switch (strFileExtension.Substring(1))
{
case "avi":
strType = "video/avi";
break;
case "bmp":
strType = "application/x-bmp";
break;
case "css":
strType = "text/css";
break;
case "dbf":
strType = "application/x-dbf";
break;
case "doc":
case "dot":
strType = "application/msword";
break;
case "dwg":
strType = "application/x-dwg";
break;
case "dxf":
strType = "image/vnd.dxf ";
break;
case "gif":
strType = "image/gif";
break;
case "hrf":
strType = "application/x-hrf";
break;
case "htc":
strType = "text/x-component";
break;
case "htm":
case "html":
case "jsp":
strType = "text/html";
break;
case "ico":
strType = "image/x-icon";
break;
case "img":
strType = "application/x-img";
break;
case "java":
strType = "java/*";
break;
case "jfif":
case "jpe":
case "jpeg":
case "jpg":
strType = "image/jpeg";
break;
case "js":
strType = "application/x-javascript";
break;
case "m3u":
strType = "audio/mpegurl";
break;
case "m4e":
strType = "video/mpeg4";
break;
case "mdb":
strType = "application/msaccess";
break;
case "mfp":
strType = "application/x-shockwave-flash";
break;
case "mid":
case "midi":
strType = "audio/mid";
break;
case "movie":
strType = "video/x-sgi-movie";
break;
case "mp3":
strType = "audio/mp3";
break;
case "mp4":
strType = "video/mpeg4";
break;
case "mpd":
strType = "application/vnd.ms-project";
break;
case "mpe":
strType = "video/x-mpeg";
break;
case "mpeg":
case "mpg":
strType = "video/mpg";
break;
case "pdf":
strType = "application/pdf";
break;
case "png":
strType = "image/png";
break;
case "ppt":
case "pps":
strType = "application/vnd.ms-powerpoint";
break;
case "rmvb":
strType = "application/vnd.rn-realmedia-vbr";
break;
case "rms":
strType = "application/vnd.rn-realmedia-secure";
break;
case "rm":
strType = "application/vnd.rn-realmedia";
break;
case "tif":
strType = "image/tiff";
break;
case "vss":
strType = "application/vnd.visio";
break;
case "wav":
strType = "audio/wav";
break;
case "wma":
strType = "audio/x-ms-wma";
break;
case "xls":
strType = "application/vnd.ms-excel";
break;
case "xml":
case "xsd":
case "tsd":
strType = "text/xml";
break;
case "swf":
strType = "application/x-shockwave-flash";
break;
case "txt":
strType = "text/plain";
break;
case "wmv":
strType = "audio/x-ms-wmv";
break;
case "wmx":
strType = "video/x-ms-wmx";
break;
}
}
return strType;
}
}
fellowcheng 2008-07-15
  • 打赏
  • 举报
回复
单独写个下载页面吧
using System;
using System.Data;
using System.Configuration;
using System.Collections;
using System.Web;
using System.Web.Security;
using System.Web.UI;
using System.Web.UI.WebControls;
using System.Web.UI.WebControls.WebParts;
using System.Web.UI.HtmlControls;
using System.IO;

public partial class DownLoadAttFile : System.Web.UI.Page
{
protected void Page_Load(object sender, EventArgs e)
{
string url = System.Configuration.ConfigurationManager.AppSettings["VisualUrl"];
int access = 0;
if (Request["fileid"] != null && Request["type"] != null)
{
//附件ID提供2种形式解析,guid和int
bool isGuid = true;
if (Request["fileid"] != null && Request["type"] != null)
{
try
{
if (!isGuid)
{
int fid = 0;
int.TryParse(Request["fileid"], out fid);
if (fid > 0)
DownFile(fid, Request["type"]);
}
else
{
Guid gid = new Guid(Request["fileid"]);
DownFile(gid, Request["type"]);
}
}
catch
{
}
}
}
}

protected void DownFile(Guid fileID,string strType)
{
string strFileFullName = null;
//获取文件路径

FileInfo file = new FileInfo(strFileFullName);
if (file.Exists)
{
string strContentType = GetContentType(file.Extension.ToLower());

Response.Clear();
Response.ClearHeaders();
Response.ClearContent();
Response.ContentType = strContentType;
Response.AddHeader("Content-disposition", "attachment; filename=" + HttpUtility.UrlEncode(file.Name));
Response.WriteFile(strFileFullName);
Response.Flush();
Response.End();
}
else
Response.Redirect("~/Error/NoFind.aspx");
}

Martin-月影 2008-07-15
  • 打赏
  • 举报
回复
提供2种方法:
1:gridview 中显示的字段直接链接到 文件 target=_Bkank 打开新窗口下载

2:就是你说的传ID 过去,在一个统一的新页面中下载
Code

public static void Download(Page page, string FilePath)
{
string FileName = string.Empty;
string FileNameWithOutExt = System.IO.Path.GetFileNameWithoutExtension(FilePath);
string Ext = System.IO.Path.GetExtension(FilePath);
string[] name = FileNameWithOutExt.Split('^');

FileName = name[0] + Ext;

FileStream myFile = new FileStream(FilePath, FileMode.Open, FileAccess.Read, FileShare.ReadWrite);
BinaryReader br = new BinaryReader(myFile);
page.Response.AddHeader("Accept-Ranges", "bytes");
page.Response.Buffer = false;
long fileLength = myFile.Length;
long startBytes = 0;

int pack = 51200; //10K bytes
int sleep = 20; //每秒50次 即50*10K bytes每秒,
//int sleep = (int)Math.Floor(1000 * pack / _speed) + 1;
if (page.Request.Headers["Range"] != null)
{
page.Response.StatusCode = 206;
string[] range = page.Request.Headers["Range"].Split(new char[] { '=', '-' });
startBytes = Convert.ToInt64(range[1]);
}
page.Response.AddHeader("Content-Length", (fileLength - startBytes).ToString());
if (startBytes != 0)
{
page.Response.AddHeader("Content-Range", string.Format(" bytes {0}-{1}/{2}", startBytes, fileLength - 1, fileLength));
}
page.Response.AddHeader("Connection", "Keep-Alive");
page.Response.ContentType = "application/octet-stream";
page.Response.AddHeader("Content-Disposition", "attachment;filename=" + HttpUtility.UrlEncode(FileName, System.Text.Encoding.UTF8));

br.BaseStream.Seek(startBytes, SeekOrigin.Begin);
int maxCount = (int)Math.Floor(Convert.ToDouble((fileLength - startBytes) / pack)) + 1;

for (int i = 0 ; i < maxCount ; i++)
{
if (page.Response.IsClientConnected)
{
page.Response.BinaryWrite(br.ReadBytes(pack));
Thread.Sleep(sleep);
}
else
{
i = maxCount;
}
}
br.Close();
myFile.Close();
}

62,173

社区成员

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

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

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

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