asp.net2.0中的文件下载问题?

wyxdrqc 2009-04-20 12:57:21
现有一些.pdf文件存放在一文件夹中,需要实现运行某一页面时,显示出该文件夹下的所有.pdf文件,并能双击下载。那位大哥能给出代码,小弟急用,谢谢了!
...全文
77 6 打赏 收藏 转发到动态 举报
写回复
用AI写文章
6 条回复
切换为时间正序
请发表友善的回复…
发表回复
浪漫幕末 2009-04-20
  • 打赏
  • 举报
回复
将文件以资源管理器的方式显示出来:WebDav的另一种功能,需要开启IIS的目录浏览功能。效果很好的,双击显示的文件就可以编辑了,同样需要开启上面说的那些才能编辑保存。
<!DOCTYPE html PUBLIC "-//W3C//DTD XHTML 1.0 Transitional//EN" "http://www.w3.org/TR/xhtml1/DTD/xhtml1-transitional.dtd">
<html xmlns="http://www.w3.org/1999/xhtml" >
<head>
<title>Test</title>
<script type="text/javascript">
var contextPath ="autoess_sz/webdav/"; //"<%=request.getContextPath()%>";
// 通过webDav操作Office文件
// nMod 1 查看
// nMod 2 编辑,默认
// nMod 3 创建
function fnWebDAVFile(szPath, nMod)
{
try{
"undefined" == typeof contextPath && (contextPath = '');
nMod || (nMod = 2);
if(!fnWebDAVFile.oSharePoint)
{
// 操作系统必须安装Office和其SharePoint组件才能使用,默认已经安装
try{fnWebDAVFile.oSharePoint = new ActiveXObject("SharePoint.OpenDocuments.3")}catch(e){
try{fnWebDAVFile.oSharePoint = new ActiveXObject("SharePoint.OpenDocuments.2")}catch(e){
try{fnWebDAVFile.oSharePoint = new ActiveXObject("SharePoint.OpenDocuments.1")}catch(e){
alert("你没有安装Office的SharePoint.OpenDocuments组件");
}
}
}
}
var o = fnWebDAVFile.oSharePoint, l = window.location,
s = l.protocol + "//" + l.host + "/" +contextPath;
szPath = s + szPath;
if(1 == nMod)
o.ViewDocument(szPath);
else if(3 == nMod)
o.CreateNewDocument(szPath, arguments[2]);
else o.editDocument(szPath);
}catch(e){alert(e.message)}
}
var oDiv = document.getElementById("MyDIv");
function fnOpenWebDAVFolder(event, szPath)
{
try{
"undefined" == typeof contextPath && (contextPath = '');
var e = event || window.event || {}, o = oDiv || e.target || e.srcElement,l = window.location,
s = l.protocol + "//" + l.host + "/" + contextPath;
o.navigateFrame(s + szPath, "jcoreWebDAV");
}catch(e){alert(e.message)}
}
</script>
</head>
<body>
<input type="button" value="Test" onclick="fnWebDAVFile('abc.doc',2)" />
<div id="MyDIv" style="cursor:pointer;behavior:url(#default#httpFolder);" onclick="fnOpenWebDAVFolder(event,'')">点这里打开WebDAV Folder
</div>
<iframe style="width:100%;height:120px" id="jcoreWebDAV" name="jcoreWebDAV"></iframe>
<label>1111111111</label>
<label>333333333333333</label>
<label>222222222</label>
<br/>
</body>
</html>

更多关于webdav请查看:http://www.webdav.org/
http://hi.baidu.com/shenqb/blog/item/55b5690e14429dcb7acbe12e.html
浪漫幕末 2009-04-20
  • 打赏
  • 举报
回复

just look at:
http://hi.baidu.com/daijun2007/blog/item/168616df439fbb1b49540301.html

webdav服务可以很好实现那个问题,主要问题就是只有IE支持
readfuture 2009-04-20
  • 打赏
  • 举报
回复
顶3楼
wuyq11 2009-04-20
  • 打赏
  • 举报
回复
遍历文件夹,绑定到gridview实现下载
<asp:GridView ID="GridView1" runat="server" Width="632px" AutoGenerateColumns="False">
<Columns>
<asp:TemplateField HeaderText="文件名">
<ItemTemplate>
<a href='<%#Eval("FileShowPath") %>' target="_blank"><%#Eval("FileName") %></a>
</ItemTemplate>
</asp:TemplateField>
<asp:TemplateField HeaderText="文件类型">
<ItemTemplate>
<%#Eval("FileType") %>
</ItemTemplate>
</asp:TemplateField>
<asp:TemplateField HeaderText="文件大小">
<ItemTemplate>
<%#Eval("FileSize") %>
</ItemTemplate>
</asp:TemplateField>
<asp:TemplateField HeaderText="文件修改时间">
<ItemTemplate>
<%#Eval("FileLastWrite") %>
</ItemTemplate>
</asp:TemplateField>
</Columns>
</asp:GridView>

public class AppCom
{
public AppCom()
{

}
private string fileName;

public string FileName
{
get { return fileName; }
set { fileName = value; }
}
private string fileType;

public string FileType
{
get { return fileType; }
set { fileType=value; }
}
private string fileSize;

public string FileSize
{
get { return fileSize; }
set { fileSize = value; }
}
private string fileLastWrite;
public string FileLastWrite
{
get { return fileLastWrite; }
set { fileLastWrite = value; }
}
private string fileShowPath;
public string FileShowPath
{
get { return fileShowPath; }
set { fileShowPath = value; }
}
}
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;
using System.Collections.Generic;
using System.Text.RegularExpressions;
public partial class UploadFileManage : System.Web.UI.Page
{
protected void Page_Load(object sender, EventArgs e)
{
if (!IsPostBack)
{
this.GridView1.DataSource = bindGridView();
this.GridView1.DataBind();
}
}
private List<AppCom> bindGridView()
{
string strPath = Server.MapPath(@"~/Images/");
string showPath = AppDomain.CurrentDomain.BaseDirectory.Substring(0,AppDomain.CurrentDomain.BaseDirectory.Length-1);
showPath=showPath.Substring(showPath.LastIndexOf("\\")+1);
showPath = @"http://" + Request.Url.Authority + "/"+showPath + "/Images/";
List<AppCom> fileList = new List<AppCom>();
DirectoryInfo di = new DirectoryInfo(strPath);
FileInfo[] files;
try
{
files = di.GetFiles();
foreach (FileInfo fi in files)
{
AppCom ac = new AppCom();
ac.FileName = fi.Name;
ac.FileSize = Convert.ToString(fi.Length);
ac.FileType = fi.Extension;
ac.FileLastWrite = fi.LastWriteTime.ToString();
ac.FileShowPath = showPath + fi.Name;
fileList.Add(ac);
}
return fileList;
}
catch (Exception)
{

throw;
}
}
protected void GridView1_RowDeleting(object sender, GridViewDeleteEventArgs e)
{

string fileName = ((DataBoundLiteralControl)(this.GridView1.Rows[e.RowIndex].Cells[0].Controls[0])).Text.Trim();
//实现文件下载
}
}

itcrazyman 2009-04-20
  • 打赏
  • 举报
回复
mark up 帮顶,网上类似代码挺多的,遍历一下目录而已
'获取css目录下所有文件夹名称
Dim cspath As String = Server.MapPath("css")
If System.IO.Directory.Exists(cspath) Then
Dim dir() As String = System.IO.Directory.GetDirectories(cspath)
For i = 0 To dir.Length - 1
Dim mc() As String = dir(i).Split("\")
mblx.Items.Add(mc(mc.Length - 1))
Next
Else
Response.Write(cspath)
Response.Write("此文件夹不存在!不能修改模板!")
Me.Button1.Disabled = True
Exit Sub
End If
CeShenBeiWang 2009-04-20
  • 打赏
  • 举报
回复
放到此网页的目录下 就可以了 用http 协议下载

62,267

社区成员

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

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

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

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