zoukankan      html  css  js  c++  java
  • 把文件从服务器的文件夹中下载下来 弹出另存为的对话框

    如果直接引用文件的下载地址(url)会直接打开文件,并不会弹出另存为的对话框(对于已知MIME).

    找到两种方法.(前面写过一篇,从数据库上传,下载的文章,正好和这个做个对应.)

    第一种是最一般的.

            // Identify the file to download including its path.
            string filepath = Server.MapPath("softfile/this.rar");

            // Identify the file name.
            string filename = System.IO.Path.GetFileName(filepath);

            Response.Clear();

            // Specify the Type of the downloadable file.
            Response.ContentType = "application/octet-stream";

            // Set the Default file name in the FileDownload dialog box.
            Response.AddHeader("Content-Disposition", "attachment; filename=" + filename);

            Response.Flush();

            // Download the file.
            Response.WriteFile(filepath);

    第二种是,用了asp.net2.0版中,新提供的一个方法TransmitFile().

    将指定的文件直接写入 HTTP 响应输出流,而不在内存中缓冲该文件。 
    这么做的好处就是解决了writefile()的,输出时会占用服务器大量内存.效率低下,不能下载大文件的问题.

    下面是一个小例子.
            string filepath = Server.MapPath("softfile/this.rar");
            string filename = System.IO.Path.GetFileName(filepath);                
            Response.Clear();
            Response.ContentType = "application/octet-stream";
            Response.AppendHeader ("Content-Disposition", "attachment;filename="+filename );
    //这里的filename可以输出时自定义,不一定用原来的.
            Response.TransmitFile(filepath );
            Response.Flush();
            Response.Close();

    来源:http://hi.baidu.com/beyoung/blog/item/ba2a6a604bde3844ebf8f898.html

  • 相关阅读:
    Canvas
    Web API 接口-JavaScript全部api接口文档
    编程中的命名设计那点事
    线程池的使用
    SRW锁的使用
    内存屏障
    VC用Beep整几首歌听听~~~
    简单的多线程并发同步演示(4种同步方法)
    C语言生成程序问题
    文件操作(输出倒数第二行、逆序输出)
  • 原文地址:https://www.cnblogs.com/hubj/p/1291980.html
Copyright © 2011-2022 走看看