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

转载 2011年01月20日 22:59:00

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#文件下载的四种方法~有文件流,有WriteFile等

using System; using System.Data; using System.Configuration; using System.Web; using System.Web....

C# 文件与二进制互转数据库写入读出

//这个方法是浏览文件对象 private void button1_Click(object sender, EventArgs e) { /...

我是如何成为一名python大咖的?

人生苦短,都说必须python,那么我分享下我是如何从小白成为Python资深开发者的吧。2014年我大学刚毕业..

C#下文件转换到二进制流再到十六进制的转换流程

C#下文件转换到二进制流再到十六进制的转换流程

c#将pdf文件转换成flash swf

来自:http://outofmemory.cn/code-snippet/1930/c-jiang-pdf-file-zhuanhuancheng-flash-swf 主要是为了在线浏览pdf文件...

C# 将PDF文件转换为word格式

Pdf(Portable Document Format)意为“便携式文档格式”,是现在最流行的文件格式之一,它有很多优点如:尺寸较小、阅读方便、操作系统平台通用等,非常适合在网络上传播和使用。如今在...
返回顶部
收藏助手
不良信息举报
您举报文章:深度学习:神经网络中的前向传播和反向传播算法推导
举报原因:
原因补充:

(最多只允许输入30个字)