1. 程式人生 > 實用技巧 >頁面A標籤連線下載: 文字( 格式: .txt ) PDF(格式: .pdf )

頁面A標籤連線下載: 文字( 格式: .txt ) PDF(格式: .pdf )

 //JS: 
//拼接字串,通過點選  “下載”  呼叫JS函式,下載 .txt、.pdf、以及其他檔案( .xls、.xlsx、.doc、.docx等) 
  var value = rowData[0];
  var name = rowData[2];
  var ext = rowData[3];
       if (ext == ".txt") {
        return "<a target=\"_blank\"  title='點選下載檔案' onclick=\"OnClickText('" + value + "','" + ext + "','"+name+"');\"  style=\"cursor:pointer;\">" + " 下載" + "</a>";
         } 
else if (ext == ".pdf") { return "<a target=\"_blank\" id=\"links\" title='點選下載檔案' download onClick=\"OnClickPdf('" + value + "','" + ext + "');\" style=\"cursor:pointer;\">" + " 下載" + "</a>"; } else { return "<a target=\"_blank\" href=\"/Resource/UploadTrain/" + value + ext + "\" title='點選下載檔案' style=\"cursor:pointer;\" >" + " 下載" + "</a>"; }
<script type="text/javascript">
//a連結 點選文字(.txt) 下載 呼叫的函式
        function OnClickText(filename, text,name) {
            //獲取文字(txt),內容:
            var _filename = filename + text;
            var _filename0 = name;//獲取文字名稱
            //找一個標籤放一個位置
            getAjax("TrainAttment.aspx", "action=OnLoadFile&txtName=" + _filename, function
(data) { $("#output").val(data); //獲取文字內容 }); var _text = $("#output").val(); download(_filename, _text); //download("data.txt", "hello word!");  // 呼叫 } //a連結 點選下載(.pdf)檔案 function OnClickPdf(filename, text) { var pdf = $("#downPDF").val(); document.getElementById('links').href = "../Resource/UploadTrain/" + filename + text; }; //a連結 獲取文字【例如: 測試001.txt】下載 內容格式 function download(filename, text) { var pom = document.createElement("a"); pom.setAttribute( "href", "data:text/plain;charset=utf-8," + encodeURIComponent(text) ); pom.setAttribute("download", filename); if (document.createEvent) { var event = document.createEvent("MouseEvents"); event.initEvent("click", true, true); pom.dispatchEvent(event); } else { pom.click(); } }; </script>
//c#
 string active = HttpContext.Current.Request["action"];  
 switch (active)
            {
                case "OnLoadFile":
                    string txtName = HttpContext.Current.Request["txtName"];
                    string Path = Server.MapPath("../Resource/UploadTrain/" + txtName);
                    ReadTxtContent(Path);
                    Response.End();
                    break;
                default:
                    break;
            }

        #region 閱讀檔案文字內容
        public void ReadTxtContent(string Path)
        {
            StreamReader sr = new StreamReader(Path, Encoding.Default);
            string content;
            while ((content = sr.ReadLine()) != null)
            {
                string _content = sr.ReadToEnd();//獲取文字的全部內容
                content += _content;
                Response.Write(content);
            }
        }
        #endregion
 <tr>
    <td>
       <%--文字域:存放文字內容--%>
       <textarea  id="output" style="width:800px;height:60px;display:none; "></textarea>
       <input  id="downPDF" style="width:800px;height:60px;display:none;"/><!-- 隱藏域:存放處室名字,供下載PDF作判斷 -->
     </td>
  </tr>