读取Doc或PDF文件内容

下面的方法,读出来 的内容是Json格式 的,具体 内容,需要分析一下Json的内容,看看哪部分数据是你想要的,注意灵活运用哦。

方法如下:

第一步,读取文件流,最终 转换成base64string

 

                    var fileStream = new FileStream(item.FullName, FileMode.Open, FileAccess.Read);
                    byte[] data = new byte[(int)fileStream.Length];
                    fileStream.Read(data, 0, data.Length);
                    var cont = Convert.ToBase64String(data);

 

第二步,使用上面的cont

       private const String host = "http://jljxjk.market.alicloudapi.com";
        private const String path = "/aliyunapp/aliyunservice.aspx";
        /// <summary>
        /// 简历解析
        /// </summary>
        /// <param name="input"></param>
        /// <returns></returns>
        public async Task<string[]> ConvertFileToFileds(string pdfBase64String)
        {
            try
            {
                String url = host + path; ;
                HttpWebRequest httpRequest = null;
                HttpWebResponse httpResponse = null;

                string jsonString = "cid=1&content=" + pdfBase64String + "&ext=pdf";
                #region 
                httpRequest = (HttpWebRequest)WebRequest.Create(url);

                httpRequest.Method = "POST";
                httpRequest.Headers.Add("Authorization", "APPCODE " + "换成你自己的 appcode");
                //根据API的要求,定义相对应的Content-Type
                httpRequest.ContentType = "application/json; charset=UTF-8";
                httpRequest.Headers.Add("Content-Type", "application/json");
                if (0 < jsonString.Length)
                {
                    byte[] data = Encoding.UTF8.GetBytes(jsonString);
                    using (Stream stream = httpRequest.GetRequestStream())
                    {
                        stream.Write(data, 0, data.Length);
                    }
                }
                try
                {
                    httpResponse = (HttpWebResponse)httpRequest.GetResponse();
                }
                catch (WebException ex)
                {
                    httpResponse = (HttpWebResponse)ex.Response;
                }
                Stream st = httpResponse.GetResponseStream();
                StreamReader reader = new StreamReader(st, Encoding.GetEncoding("utf-8"));
                var content = await reader.ReadToEndAsync();

                var pdfcontentobj = Newtonsoft.Json.JsonConvert.DeserializeObject<PdfContent>(content);//分析你自己的返回结果,自定义自己要反序列化的对象,然后接下来的代码,换成你自己的业务处理即可

                string pdfcontent = pdfcontentobj.Original;
                Logger.Info("解析后的内容=" + pdfcontent);
                var items = pdfcontent.Split("\u000d\u000a");
                return items;

                #endregion
            }
            catch (Exception ex)
            {
                //  Logger.Error("ConvertFileToFileds Error" + input.ToJson(), ex);
            }
            //Logger.Info("ConvertFileToFileds end output..." + output.ToJson());
            //return output;
            return null;
        }

 

 

 

评论
添加红包

请填写红包祝福语或标题

红包个数最小为10个

红包金额最低5元

当前余额3.43前往充值 >
需支付:10.00
成就一亿技术人!
领取后你会自动成为博主和红包主的粉丝 规则
hope_wisdom
发出的红包
实付
使用余额支付
点击重新获取
扫码支付
钱包余额 0

抵扣说明:

1.余额是钱包充值的虚拟货币,按照1:1的比例进行支付金额的抵扣。
2.余额无法直接购买下载,可以购买VIP、付费专栏及课程。

余额充值