首页 技术 正文
技术 2022年11月8日
0 收藏 876 点赞 1,905 浏览 3815 个字

————————————————————–后台代码——————————————

    public JsonResult ImportPDF(Int64 id)
        {
            try
            {
                Guid currentGuid = Guid.NewGuid();

if (Request.Files[“FileData”].HasFile())
                {
                    HttpPostedFileBase file = Request.Files[“FileData”];
                    //if (file.InputStream.Length > 16*1024*1024)
                    //{
                    //    throw new Exception(“文件过大,导入不成功!”);
                    //}
                    CreateFolder();
                    string path = Server.MapPath(“/Ebook”);
                    string fileName = “1.pdf”;

//Directory.CreateDirectory(path);
                    file.SaveAs(string.Format(@”{0}\{1}”, path, fileName));
                    PDFText(string.Format(@”{0}\{1}”, path, fileName), id, currentGuid);
                }

return Json(currentGuid.ToString(), JsonRequestBehavior.AllowGet);
            }
            catch (Exception ex)
            {
                throw ex;
            }
            
        }

/// <summary>
        /// 获取导入PDF的进度
        /// </summary>
        /// <returns></returns>
        public JsonResult GetPdfProgress(string guidStr)
        {
            try
            {
                Guid guid = new Guid(guidStr.Trim(‘”‘));
                if (pdfProDic.ContainsKey(guid))
                {
                    return Json(new { guidKey = guidStr, proVal = pdfProDic[guid] }, JsonRequestBehavior.AllowGet);
                }
                return Json(new {guidKey = guidStr}, JsonRequestBehavior.AllowGet);
            }
            catch (Exception ex)
            {
                throw ex;
            }

}
        
        //注意要使用静态(字典与GUID为了应对多人同时访问)
        static Dictionary<Guid,int> pdfProDic=new Dictionary<Guid, int>();
        
        public void PDFText(string fileName, Int64 id,Guid guid)
        {
            System.Threading.Tasks.Task.Factory.StartNew(user =>
            {
                try
                {
                    pdfProDic.Add(guid,0);
                    Domain.UserModel.User currentUser = user as Domain.UserModel.User;
                    if (user != null)
                    {
                        #region 执行pdf导入数据库
                        //注意加载PDF文件过大会出错
                        PDDocument doc = PDDocument.load(fileName);
                        PDFTextStripper pdfStripper = new PDFTextStripper();

short currentPage = GetMaxPageNumber(id);

if (currentPage < 10000)
                            currentPage = 10000;

float j = 0;
                        int progress = 0;
                        for (int i = 0; i < doc.getNumberOfPages(); i++)
                        {
                            currentPage++;

//索引是从0开始,第一页表示0~1
                            pdfStripper.setStartPage(i);
                            pdfStripper.setEndPage(i + 1);
                            String pdfStr = pdfStripper.getText(doc);

var target = EntAppFrameWorkContext.Application.ExtenedT<Ebook, Int64, EbookAppExt>().
                                CreatePage(
                                    id,
                                    currentPage,
                                    pdfStr,
                                    currentUser);
                            j = i+1;
                            progress = (int)((j / doc.getNumberOfPages()) * 100);
                            if (Convert.ToInt32(j) >= doc.getNumberOfPages())
                            {
                                progress = 100;
                                //为了性能的提升,这时候进行排序
                                EntAppFrameWorkContext.Application.ExtenedT<Ebook,Int64,EbookAppExt>().InitEbookPage(id);
                            }
                            pdfProDic[guid] = progress;
                        }
                        doc.close();
                        #endregion
                    }
                }
                catch (Exception ex)
                {
                    throw ex;
                }
                
            }, base.CurrentUser);
            
        }

相关推荐
python开发_常用的python模块及安装方法
adodb:我们领导推荐的数据库连接组件bsddb3:BerkeleyDB的连接组件Cheetah-1.0:我比较喜欢这个版本的cheeta…
日期:2022-11-24 点赞:878 阅读:9,488
Educational Codeforces Round 11 C. Hard Process 二分
C. Hard Process题目连接:http://www.codeforces.com/contest/660/problem/CDes…
日期:2022-11-24 点赞:807 阅读:5,903
下载Ubuntn 17.04 内核源代码
zengkefu@server1:/usr/src$ uname -aLinux server1 4.10.0-19-generic #21…
日期:2022-11-24 点赞:569 阅读:6,736
可用Active Desktop Calendar V7.86 注册码序列号
可用Active Desktop Calendar V7.86 注册码序列号Name: www.greendown.cn Code: &nb…
日期:2022-11-24 点赞:733 阅读:6,487
Android调用系统相机、自定义相机、处理大图片
Android调用系统相机和自定义相机实例本博文主要是介绍了android上使用相机进行拍照并显示的两种方式,并且由于涉及到要把拍到的照片显…
日期:2022-11-24 点赞:512 阅读:8,127
Struts的使用
一、Struts2的获取  Struts的官方网站为:http://struts.apache.org/  下载完Struts2的jar包,…
日期:2022-11-24 点赞:671 阅读:5,289