Ajax reads txt and displays the txt content by page.
I will share with you the core code of ajax reading txt and paging display of txt content. I will not talk much about it, but I will paste the Code directly.
Function TransferString (content) {var string = content; try {string = string. replace (/\ r \ n/g, "<BR>") string = string. replace (/\ n/g, "<BR>"); string = string. replace (/[]/g, "") string = string. replace (/\ +/g, "")} catch (e) {alert (e. message);} return string;} var pageIndex = 1; var id = $ ("# aId "). val (); var url = $ ('# urlarticlecontent '). val (); var txt = ''; var pageSize = @ (ConfigurationManager. appSettings ["ArticlepageSi Ze "]); var page = 5; var pageCount = 0; $ (function () {if (url! = "") {Txt = ($. ajax ({url: url, async: false}). responseText; if (txt. indexOf (' http://www.w3.org/1999/xhtml ') =-1) {pageCount = Math. ceil (txt. length/pageSize); $ ("# PageCount" ).html(pagecount{}('{word'{.html (TransferString (txt. substring (0, pageSize); $ ('# demo '). pagination ({dataSource: function (done) {var result = []; for (var I = 1; I <pageCount; I ++) {result. push (I) ;}done (result) ;}, pageCount: pageCount, pageSize: 5, showGoInput: true, showGoButton: true, callback: function (data, pagination) {// template me Thod of yourselfvar html = template(data‑embedded datacontainer.html (html) ;}})} else {txt = '';}}) function GoHead () {GoPage (1);} function NextClick () {if (pageIndex <pageCount) {pageIndex = pageIndex + 1;} else {pageIndex = pageCount;} GoPage (pageIndex); $ ("# pageCurrent" ).html (pageIndex );} function backClick () {if (pageIndex> 1) {pageIndex = pageIndex-1; GoPage (pageIndex); $ ("# pageCurrent" ).html (pageIndex) ;}} Function GoPageNew () {var page =$ ("# pageGo "). val (); GoPage (page); opts. current = page;} function GoPage (pageIndex) {if (pageIndex = 0) {pageIndexStr = $ ('# gopage '). val (); if (pageIndexStr = undefined) {return false;} pageIndexStr = pageIndexStr. trim (); var pageIndex = parseInt (pageIndexStr) ;}$ ('# page' + pageIndex ). siblings (). removeClass ('active'); $ ('# page' + pageIndex ). addClass ('active'); if (pageIndex = 1) {if (! $ ('# Backclick '). hasClass ('Disabled ') {$ (' # backclick '). addClass ('Disabled ') ;}} else {$ (' # backclick '). removeClass ('Disabled ');} if (pageIndex = pageCount) {if (! $ ('# Nextclick '). hasClass ('Disabled ') {$ (' # nextclick '). addClass ('Disabled ') ;}} else {$ (' # nextclick '). removeClass ('Disabled ');} var pageTxt = txt. substring (pageIndex-1) * pageSize, pageIndex * pagesize);(('{word'{.html (TransferString (pageTxt); $ ('html, body '). animate ({'scrolltop ': 0}, 0); $ ("# pageCurrent" pai.html (pageIndex);} function PageGo () {if ($ ('. jump-ipt '). val ()! = '') {GoPage ($ ('. jump-ipt'). val () }}@{ if (null! = ViewBag. Ariticle) {DataRow dr = ViewBag. Ariticle; if (null! = Dr) {ViewBag. title = dr ["Title"]; <div class = "container"> <div class = "row"> <div class = "col-xs-12"> <div class = "breadcrumb_block"> <span class = "tit"> <a href = "/web/sciencefiction/index"> Popular Science IP </a> </span> <ol class = "breadcrumb"> <li class = "active"> @ dr ["Title"] </li> <li class = "active"> online trial </li> </ol> <div class = "clear"> </div> </div> <div class = "col-sm-8"> <input type = "hidden" name = "aId" id = "aId" value = "@ Req Uest ["id"] "/> <div class =" read_book ">
Some processing is required when uploading a txt file. Otherwise, only UTF8 txt files can be displayed.
/// <Summary> // File Upload /// </summary> /// <returns> </returns> public JsonResult UploadFiles () {HttpPostedFileBase postFile = HttpContext. request. files ["filesave"]; if (postFile! = Null) {string fileName = Path. getFileName (postFile. fileName); string fileExt = Path. getExtension (fileName. toLower (); string fileSize = postFile. contentLength. toString (); long fileNameTem = Common. generateLongId (); string sPath = string. format ("/Data/Material/1_01__1_11_.txt", fileNameTem. toString (), fileName. replace (". txt "," "); // Guid. newGuid (). toString (). replace ("-", "") string viewUrl = string. empty; st Ring g = Guid. NewGuid (). ToString (); if (! Directory. Exists (Server. MapPath ("~ ") +"/Data/Material ") Directory. CreateDirectory (Server. MapPath ("~ ") +"/Data/Material "); string p = Server. mapPath (sPath); postFile. saveAs (p); if (System. IO. file. exists (p) {StreamReader sr = new StreamReader (p, System. text. encoding. default); String input = sr. readToEnd (); sr. close (); StreamWriter sw = new StreamWriter (p, false, System. text. encoding. UTF8); sw. writeLine (input); sw. close () ;}return Json (new {FilePath = sPath, FileName = fileName, viewurl = viewUrl, fileSize = fileSize });} else {return Json (new {FilePath = ""});}}
In addition, js is introduced to the homepage.
<Script src = "~ /Scripts/web/jquery. pagination. js "> </script> <link href = "~ /Content/jquery.pagination.css "rel =" stylesheet "/>/*** pagination page plug-in * @ version 1.1.2 * @ author mss * @ url http://maxiaoxiang.com/plugin/pagination.html * @ E-mail 251445460@qq.com ** @ call Method * $ (selector ). pagination (); ** @ Update log * 2016-07-25: fixed the click repetition event */; (function ($, window, document, undefined) {// Configuration Parameter var defaults = {totalData: 0, // total number of data entries showData: 0, // number of entries displayed per page pageCount: 9, // total number of pages, the default value is 9 current: 1, // current page number prevCls: 'prev', // Previous Page classnextCls: 'Next', // next page classprevContent: 'previous page ', // nextContent: 'Next' on the previous page, // activeCls: 'active' on the next page, // coping: false on the current page, // HomePage and end page homePage: '', // homePage node content endPage:'', // end page node content count: 5, // The number of pages before and after the current page: jump: true, // jump to the specified page jumpIptCls: 'jump-ipt', // the content of the text box jumpBtnCls: 'jump-btn ', // jump button jumpBtn: 'jump ', // jump button text callback: function () {}// callback}; var Pagination = function (element, options) {// global variable var opts = options, // configure current, // current page $ document = $ (document), $ obj = $ (element ); // container/*** set the total page number * @ param int page number * @ return opts. total pageCount Page number configuration */this. setTotalPage = function (page) {return opts. pageCount = page ;};/*** obtain the total number of pages * @ return int p total number of pages */this. getTotalPage = function () {var p = opts. totalData | opts. showData? Math. ceil (parseInt (opts. totalData)/opts. showData): opts. pageCount; return p ;}; // obtain the current page this. getCurrent = function () {return current;};/*** fill data * @ param int index page number */this. filling = function (index) {var html = ''; current = index | opts. current; // the current page number var pageCount = this. getTotalPage (); if (current> 1) {// Previous Page html + = '<li onclick = GoPage (' + (current-1) + ')> <a href = "javascript:;" class = "'+ opts. PrevCls + '">' + opts. prevContent + '</a> </li>';} else {$ obj. find ('. '+ opts. prevCls) & $ obj. find ('. '+ opts. prevCls ). remove ();} if (current> = opts. count * 2 & current! = 1 & pageCount! = Opts. count) {var home = opts. coping & opts. homePage? Opts. homePage: '1'; html + = opts. coping? '<Li onclick = GoPage (' + home + ')> <a href = "javascript :; "data-page =" 1 "> '+ home +' </a> </li> <span>... </span> ': '';} var start = current-opts. count, end = current + opts. count; (start> 1 & current <opts. count) | current = 1) & end ++ (current> pageCount-opts. count & current> = pageCount) & start ++; for (; start <= end; start ++) {if (start <= pageCount & start> = 1) {if (start! = Current) {html + = '<li onclick = GoPage (' + start + ')> <a href = "javascript :; "data-page =" '+ start +' "> '+ start +' </a> </li> ';} else {html + = '<li class = "' + opts. activeCls + '"onclick = GoPage (' + start + ')> <a href =" javascript :; "> <span> '+ start +' </span> </a> </li> ';}} if (current + opts. count <pageCount & current> = 1 & pageCount> opts. count) {var end = opts. coping & opts. endPage? Opts. endPage: pageCount; html + = opts. coping? '<Span>... </span> <li onclick = GoPage ('+ pageCount +')> <a href = "javascript :; "data-page =" '+ pageCount +' "> '+ end +' </a> </li> ':'';} if (current <pageCount) {// next page html + = '<li onclick = GoPage (' + (current + 1) + ')> <a href = "javascript :; "class =" '+ opts. nextCls + '">' + opts. nextContent + '</a> </li>'} else {$ obj. find ('. '+ opts. nextCls) & $ obj. find ('. '+ opts. nextCls ). remove ();} html + = opts. ju Mp? '<Input type = "text" class = "' + opts. jumpIptCls + '"> <a href =" javascript:; "onclick = PageGo () class ="' + opts. jumpBtnCls + '">' + opts. jumpBtn + '</a>': ''{}obj.empty().html (html) ;}; // bind the event this. eventBind = function () {var self = this; var pageCount = this. getTotalPage (); // the total number of pages $ obj. off (). on ('click', 'A', function () {if ($ (this ). hasClass (opts. nextCls) {var index = parseInt ($ obj. find ('. '+ opts. active Cls ). text () + 1;} else if ($ (this ). hasClass (opts. prevCls) {var index = parseInt ($ obj. find ('. '+ opts. activeCls ). text ()-1;} else if ($ (this ). hasClass (opts. jumpBtnCls) {if ($ obj. find ('. '+ opts. jumpIptCls ). val ()! = '') {Var index = parseInt ($ obj. find ('. '+ opts. jumpIptCls ). val () ;}else {return ;}} else {var index = parseInt ($ (this ). data ('page');} self. filling (index); typeof opts. callback === 'function' & opts. callback (self) ;}); // enter the jump page number $ obj. on ('input propertychang ','. '+ opts. jumpIptCls, function () {var $ this = $ (this); var val = $ this. val (); var reg =/[^ \ d]/g; if (reg. test (val) {$ this. val (val. replace (reg, '');} (parseInt (val)> pageCount) & $ this. val (pageCount); if (parseInt (val) = 0) {// the minimum value is 1 $ this. val (1) ;}}); // press enter to jump to the specified page number $ document. keydown (function (e) {var self = this; if (e. keyCode = 13 & $ obj. find ('. '+ opts. jumpIptCls ). val () {var index = parseInt ($ obj. find ('. '+ opts. jumpIptCls ). val (); GoPage (index); // self. filling (index); // typeof opts. callback === 'function' & opts. callback (self) ;}}) ;}; // initialize this. init = function () {this. filling (opts. current); this. eventBind () ;}; this. init () ;}; $. fn. pagination = function (parameter, callback) {if (typeof parameter = 'function') {// reload callback = parameter; parameter = {};} else {parameter = parameter | |{}; callback = callback | function () {};} var options =$. extend ({}, defaults, parameter); return this. each (function () {var pagination = new Pagination (this, options); callback (pagination) ;};}) (jQuery, window, document );
The above section describes how to use Ajax to read txt files and display the txt content by page. I hope it will be helpful to you. If you have any questions, please leave a message for me, the editor will reply to you in a timely manner. Thank you very much for your support for the help House website!