C#下载文件和将文件转换为数据流下载的示例

protected void Page_Load(object sender, EventArgs e)
    {
        string file = Request["file"];
        if (string.IsNullOrEmpty(file))
            return;
        string filepath = Server.MapPath("~/Download/" + file);
        if (File.Exists(filepath))
            //DownLoadStreamFile(filepath);
            ToOpen(filepath, "");
        Response.End();
    }

    /// <summary>
    /// 下载文件
    /// </summary>
    /// <param name="serverfilpath"></param>
    /// <param name="filename"></param>
    public static void ToDownload(string serverfilpath, string filename)
    {
        FileStream fileStream = new FileStream(serverfilpath, FileMode.Open);
        long fileSize = fileStream.Length;
        HttpContext.Current.Response.ContentType = "application/octet-stream";
        HttpContext.Current.Response.AddHeader("Content-Disposition", "attachment; filename=/"" + UTF_FileName(filename) + "/";");
        ////attachment --- 作为附件下载
        ////inline --- 在线打开
        HttpContext.Current.Response.AddHeader("Content-Length", fileSize.ToString());
        byte[] fileBuffer = new byte[fileSize];
        fileStream.Read(fileBuffer, 0, (int)fileSize);
        HttpContext.Current.Response.BinaryWrite(fileBuffer);
        fileStream.Close();
        HttpContext.Current.Response.End();
    }

    /// <summary>
    /// 打开文件
    /// </summary>
    /// <param name="serverfilpath"></param>
    /// <param name="filename"></param>
    public static void ToOpen(string serverfilpath, string filename)
    {
        FileStream fileStream = new FileStream(serverfilpath, FileMode.Open);
        long fileSize = fileStream.Length;
        HttpContext.Current.Response.ContentType = "application/octet-stream";
        HttpContext.Current.Response.AddHeader("Content-Disposition", "inline; filename=/"" + UTF_FileName(filename) + "/";");
        HttpContext.Current.Response.AddHeader("Content-Length", fileSize.ToString());
        byte[] fileBuffer = new byte[fileSize];
        fileStream.Read(fileBuffer, 0, (int)fileSize);
        HttpContext.Current.Response.BinaryWrite(fileBuffer);
        fileStream.Close();
        HttpContext.Current.Response.End();
    }

    private static string UTF_FileName(string filename)
    {
        return HttpUtility.UrlEncode(filename, System.Text.Encoding.UTF8);
    }

    /// <summary>
    /// 下载文件数据流
    /// </summary>
    /// <param name="serverfilpath"></param>
    public static void DownLoadStreamFile(string serverfilpath)
    {
        int blocksize = 1024 * 100;
        byte[] buffer = new byte[blocksize];
        byte[] fileHeader = new byte[101];

        int startindex = 0;
        int bytesread = 0;
        int bytesfinish = 0;
        int filesize = 0;
        string headerText = string.Empty;
        BinaryReader bReader = null;

        bReader = new BinaryReader(File.OpenRead(serverfilpath));
        filesize = Convert.ToInt32(bReader.BaseStream.Length);

        while (bytesfinish < filesize)
        {
            if (bytesfinish + blocksize < filesize)
                bytesread = bReader.Read(buffer, startindex, blocksize);
            else
                bytesread = bReader.Read(buffer, startindex, filesize - bytesfinish);

            bytesfinish += bytesread;
            HttpContext.Current.Response.OutputStream.Write(buffer, 0, bytesread);
        }
        bReader.Close();
        HttpContext.Current.Response.End();
    }

阅读更多
个人分类: c#
上一篇JS 日历控件 带小时分钟
下一篇C#自动下载并保存文件示例
想对作者说点什么? 我来说一句

没有更多推荐了,返回首页

关闭
关闭