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

ASP.NET下载文件的问题,在线等高手帮忙~~

现在在网页上放一个按钮,点击按钮要实现将查询出来的所有文件从服务器端下载到本地电脑的某个目录里。但是我用了网上的几种下载方式,均不能实现,都是只把文件下载到服务器的硬盘中了。我想,这可能是因为页面在服务器上运行的缘故。但是我要实现,将服务器端的文件下载到本地的目录里,最好是能提示保存位置,由客户自由选择,然后进行批量下载,该如何实现呢?有没有可供参考的源代码?在线等。。。 --------------------编程问答-------------------- #region DownLoad --- 下载文件

    /// <summary>
    /// 下载文件
    /// </summary>
    /// <param name="path">路径</param>
    /// <returns></returns>
    public static bool DownLoad(string path)
    {
        return DownLoad(path, "", "");
    }

    /// <summary>
    /// 下载文件
    /// </summary>
    /// <param name="path">路径</param>
    /// <param name="name">文件名</param>
    /// <param name="type">文件类型</param>
    /// <returns></returns>
    public static bool DownLoad(string path, string name, string type)
    {
        HttpRequest req = HttpContext.Current.Request;
        HttpResponse rep = HttpContext.Current.Response;
        name = string.IsNullOrEmpty(name) ? Path.GetFileName(path) : name;
        path = string.IsNullOrEmpty(type) ? path : (path.Substring(0, path.LastIndexOf(".")) + type);
        long speed = 1024 * 1024 * 100;
        try
        {
            if (!File.Exists(path)) { rep.Write("<span style=\"color:#ff0000;\">文件已丢失!</span>"); return false; }
            FileStream fs = new FileStream(path, FileMode.Open, FileAccess.Read, FileShare.ReadWrite);
            if (fs.Length > long.MaxValue) { rep.Write("<span style=\"color:#ff0000;\">请求文件过大!</span>"); return false; }
            BinaryReader br = new BinaryReader(fs);
            try
            {
                rep.AddHeader("Accept-Ranges", "bytes");
                rep.Buffer = false;
                long length = fs.Length;
                long start = 0;
                int pack = 102400;
                int sleep = (int)Math.Floor(1000.0 * pack / speed) + 1;
                if (req.Headers["Range"] != null) { rep.StatusCode = 206; string[] range = req.Headers["Range"].Split(new char[] { '=', '-' }); start = Convert.ToInt64(range[1]); }
                rep.AddHeader("Content-Length", (length - start).ToString());
                if (start != 0) { rep.AddHeader("Content-Range", string.Format("bytes {0}-{1}/{2}", start, length - 1, length)); }
                rep.AddHeader("Connection", "Keep-Alive");
                rep.ContentType = "application/octet-stream";
                rep.AddHeader("Content-Disposition", "attachment;filename=" + HttpUtility.UrlEncode(name, System.Text.Encoding.UTF8));
                br.BaseStream.Seek(start, SeekOrigin.Begin);
                int count = (int)Math.Floor((double)(length - start) / pack) + 1;
                for (int i = 0; i < count; ) { if (rep.IsClientConnected) { rep.BinaryWrite(br.ReadBytes(pack)); System.Threading.Thread.Sleep(sleep); } else { break; } i++; if (i == count) { return true; } }
            }
            catch (Exception ex) { throw ex; }
            finally
            {
                br.Close();
                fs.Close();
            }
        }
        catch { return false; }
        return false;
    }
    #endregion --------------------编程问答--------------------
引用 1 楼 aluogang 的回复:
#region DownLoad --- 下载文件

  /// <summary>
  /// 下载文件
  /// </summary>
  /// <param name="path">路径</param>
  /// <returns></returns>
  public static bool DownLoad(string path)
  {
  return Do……


刚刚我已经用你这段代码测试过了,好像是能下载文件,但是下载下来的文件不能打开。而且要指定服务器端的和本地的文件路径。因为我这里要下载的数据在IIS上的一个虚拟目录里,如果单个下载的话会很方便,问题是现在客户要求能批量下载。。。 --------------------编程问答-------------------- 学习一上,我也在学下载文件这个功能 --------------------编程问答--------------------
引用 2 楼 lanjian0819 的回复:
引用 1 楼 aluogang 的回复:
#region DownLoad --- 下载文件

/// <summary>
/// 下载文件
/// </summary>
/// <param name="path">路径</param>
/// <returns></returns>
public static bool DownLoad(string path)
{
ret……

不能打开么?你那我不知道是什么原因,我这能打开的!批量下载,我还在研究中,具体如何实现还没做好,不能给你帮助,抱歉! --------------------编程问答--------------------
  /// <summary>
  /// 下载文件
  /// </summary>
  /// <param name="path">路径</param>
  /// <returns></returns>
  public static bool DownLoad(string path)
  {
  return DownLoad(path, "", "");
  }

  /// <summary>
  /// 下载文件
  /// </summary>
  /// <param name="path">路径</param>
  /// <param name="name">文件名</param>
  /// <param name="type">文件类型</param>
  /// <returns></returns>
  public static bool DownLoad(string path, string name, string type)
  {
  HttpRequest req = HttpContext.Current.Request;
  HttpResponse rep = HttpContext.Current.Response;
  name = string.IsNullOrEmpty(name) ? Path.GetFileName(path) : name;
  path = string.IsNullOrEmpty(type) ? path : (path.Substring(0, path.LastIndexOf(".")) + type);
  long speed = 1024 * 1024 * 100;
  try
  {
  if (!File.Exists(path)) { rep.Write("<span style=\"color:#ff0000;\">文件已丢失!</span>"); return false; }
  FileStream fs = new FileStream(path, FileMode.Open, FileAccess.Read, FileShare.ReadWrite);
  if (fs.Length > long.MaxValue) { rep.Write("<span style=\"color:#ff0000;\">请求文件过大!</span>"); return false; }
  BinaryReader br = new BinaryReader(fs);
  try
  {
  rep.AddHeader("Accept-Ranges", "bytes");
  rep.Buffer = false;
  long length = fs.Length;
  long start = 0;
  int pack = 102400;
  int sleep = (int)Math.Floor(1000.0 * pack / speed) + 1;
  if (req.Headers["Range"] != null) { rep.StatusCode = 206; string[] range = req.Headers["Range"].Split(new char[] { '=', '-' }); start = Convert.ToInt64(range[1]); }
  rep.AddHeader("Content-Length", (length - start).ToString());
  if (start != 0) { rep.AddHeader("Content-Range", string.Format("bytes {0}-{1}/{2}", start, length - 1, length)); }
  rep.AddHeader("Connection", "Keep-Alive");
  rep.ContentType = "application/octet-stream";
  rep.AddHeader("Content-Disposition", "attachment;filename=" + HttpUtility.UrlEncode(name, System.Text.Encoding.UTF8));
  br.BaseStream.Seek(start, SeekOrigin.Begin);
  int count = (int)Math.Floor((double)(length - start) / pack) + 1;
  for (int i = 0; i < count; ) { if (rep.IsClientConnected) { rep.BinaryWrite(br.ReadBytes(pack)); System.Threading.Thread.Sleep(sleep); } else { break; } i++; if (i == count) { return true; } }
  }
  catch (Exception ex) { throw ex; }
  finally
  {
  br.Close();
  fs.Close();
  }
  }
  catch { return false; }
  return false;
  }
--------------------编程问答-------------------- http://blog.csdn.net/lijiangchxp2005/archive/2008/04/22/2315105.aspx
不知道这个对你有没有帮助 --------------------编程问答-------------------- 批量下载不会,来学习学习 --------------------编程问答-------------------- 网上很多 楼主 --------------------编程问答--------------------
引用 8 楼 itliyi 的回复:
网上很多 楼主


在下愚笨,找了两天了还未找到好用的示例。你那有这类的代码或者资料吗?? --------------------编程问答-------------------- 我现在在考虑用服务端先打包,再下载这样的思路。
补充:.NET技术 ,  ASP.NET
CopyRight © 2012 站长网 编程知识问答 www.zzzyk.com All Rights Reserved
部份技术文章来自网络,