下面的方法,读出来 的内容是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;
}