asp.net下载文件问题
请问各位高手,如何显示指定文件夹下的文件,并可以选择下载(能批量下载功能最好)?最好能贴上源码,谢谢了! --------------------编程问答-------------------- 不是有很多批量下载工具吗.单个连接做不到. --------------------编程问答-------------------- 这是以前上传到数据库的图片,然后在页面绑定的图片.其实和下载差不多.都是重新写的一个页面,专门用来下载写下载的代码的.//清除缓冲:
Response.Clear();
//Response.AddHeader("Content-Disposition", "attachment;filename=" + fname);
//生成图片:
bmpThumb.Save(Response .OutputStream,ImageFormat .Jpeg );
//Response.ContentType = str;
//Response.BinaryWrite(b);
Response.End();
把注释了的改过来就行了.
我想批量下载就是在数据库循环取数据就行了.
if (!Page.IsPostBack)
{
const int MaxLength=80;//最大长度.
string id = Request.QueryString["pid"];
SqlConnection con = new SqlConnection("server=localhost;uid=sa;pwd=;database=Shopping");
SqlCommand cmd = new SqlCommand("select Image,ContentType from Products where ProductID=@pid", con);
cmd.Parameters.Add(new SqlParameter("@pid", SqlDbType.Int));
cmd.Parameters[0].Value = id;
con.Open();
SqlDataReader dr = cmd.ExecuteReader();
dr.Read();
// string fname = dr.GetString(0);
byte[] b = (byte[])dr[0];
string str = dr.GetString(1);
//缩放图片:
MemoryStream ms = new MemoryStream(b);
Bitmap bmpold = new Bitmap(ms);
//计算缩小比例:
double d1;
if (bmpold.Height > bmpold.Width)
{
d1 = (double)(MaxLength / (double)bmpold.Width);
}
else
{
d1 = (double)(MaxLength / (double)bmpold.Height);
}
//产生缩图:
Bitmap bmpThumb = new Bitmap(bmpold ,(int)(bmpold .Width *d1),((int)(bmpold .Height *d1)));
con.Close();
//清除缓冲:
Response.Clear();
//Response.AddHeader("Content-Disposition", "attachment;filename=" + fname);
//生成图片:
bmpThumb.Save(Response .OutputStream,ImageFormat .Jpeg );
//Response.ContentType = str;
//Response.BinaryWrite(b);
Response.End();
//释放资源:
bmpThumb.Dispose();
bmpold.Dispose();
ms.Dispose();
} --------------------编程问答-------------------- #region 得到当前可用的文件列表
/// <summary>
/// 得到当前可用的文件列表
/// </summary>
/// <param name="IsAlert">是否需要弹出提示信息</param>
private void fn_getCurrFileList(bool IsAlert)
{
try
{
//查找xmlzip文件夹下 属于其本身UnitCoding的相关zip文件
string strXmlZipDirectory = Server.MapPath("../xmlzip/");//这里换成你要的文件夹路径
if (Directory.Exists(strXmlZipDirectory))
{
//DirectoryInfo di = new DirectoryInfo(Environment.CurrentDirectory);
DirectoryInfo di = new DirectoryInfo(strXmlZipDirectory);
FileInfo[] FI = di.GetFiles("*.zip");//只查.zip文件,这里可以选择文件后缀名
if (FI.Length > 0)
{
lst_DownLoadFileList.Items.Clear();
foreach (FileInfo tmpFI in FI)
{
ListItem tmpItem = new ListItem();
tmpItem.Text = tmpFI.Name;
lst_DownLoadFileList.Items.Add(tmpItem);
}
lst_DownLoadFileList.SelectedIndex = 0;
}
else
{
if (IsAlert)
{
Response.write("查无可以下载的文件!");
}
}
}
}
catch (Exception ex)
{
ex.ToString();
}
}
#endregion
下载显示连接即可.批量的就用下载工具 --------------------编程问答-------------------- 看到那么多代码 头晕~呵呵 --------------------编程问答-------------------- 帮顶! --------------------编程问答-------------------- 学习 --------------------编程问答-------------------- 支持 --------------------编程问答-------------------- 遍历
using System.IO;
using System.Text;
public partial class _Default : System.Web.UI.Page
{
protected void Page_Load(object sender, EventArgs e)
{
}
protected void Button1_Click(object sender, EventArgs e)
{
if (Directory.Exists(TextBox1.Text)==false)
{
Label1.Text = "该文件不存在或路径错误!";
return;
}
else
{
DirectoryInfo di = new DirectoryInfo(TextBox1.Text);
FileSystemInfo[] dis = di.GetFileSystemInfos();
if (dis.Length < 1)
{
Label1.Text = "该文件夹是空文件夹!";
}
else
{
ListBox1.Visible = true;
ListBox1.DataSource = dis;
ListBox1.DataBind();
Label1.Text = "检索成功,以上为该路径的文件和目录列表!";
}
}
}
} --------------------编程问答-------------------- 是在页面显示指定文件夹的文件,并可以选择下载啊,最好做成表格,点击连接就可以下载,请高手示教. --------------------编程问答-------------------- 学习了
补充:.NET技术 , ASP.NET