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

  • 相关阅读:
    前端——DOM
    前端——JavaScript
    前端——HTML
    初学Python——协程
    初学Python——进程
    初学Python——线程
    初学Python——Socket网络编程
    初学Python——RabbitMQ的安装
    初学Python——面向对象(二)
    muduo网络库源码学习————线程池实现
  • 原文地址:https://www.cnblogs.com/hubj/p/1291980.html
Copyright © 2011-2022 走看看