本软件为edu-paas的文档在线预览,为开源软件。
支持所有office文档在线预览。
文件类型全,转化快,跨平台
响应式预览,兼容所有访问端。

下载地址 live.edu-paas.com/dowmCenter/EDUDocumentOnlinePreviewToolV.1.zip 源程序
下载地址 live.edu-paas.com/dowmCenter/EDUFbDocumentOnlinePreviewToolV.1.zip 发布好的web

 

 1         string serverUrl = "http://office.edu-paas.com/Server/FileConvertDemo.ashx";//服务器地址
 2             string username = context.Request["username"]; // [username] 用户帐号
 3             string token = context.Request["token"]; // [token] 用户系统的token
 4             string officeurl = context.Request["officeurl"];//在线文本地址
 5             string filepath = "";
 6             if (string.IsNullOrEmpty(officeurl))
 7             {
 8                 //如果为空 判断获取本地上传
 9                 HttpPostedFile Path = HttpContext.Current.Request.Files["Filedata"];
10                 string str = DateTime.Now.Year.ToString() + DateTime.Now.Month.ToString() + DateTime.Now.Day.ToString() + DateTime.Now.Hour.ToString() + DateTime.Now.Minute.ToString() + DateTime.Now.Second.ToString() + DateTime.Now.Millisecond.ToString();
11                 string tp = System.Web.HttpContext.Current.Server.MapPath("/");
12                 filepath = tp + str + Path.FileName.Substring(Path.FileName.LastIndexOf("."));//本地文件
13                 Path.SaveAs(filepath);
14                 context.Response.Write(this.HttpUploadFile(serverUrl, filepath, username, token));
15             }
16             else
17             {
18                 context.Response.Write(this.HttpUploadFileofficeurl(serverUrl, officeurl, username, token));
19 
20 
21             }
  1   public string HttpUploadFile(string serverUrl, string filepath, string username, string token)
  2         {
  3             string content = "";
  4             try
  5             {string serverUrl = "http://office.edu-paas.com/Server/FileConvertDemo.ashx";//服务器地址  9                 // [username] 用户帐号
 10                 // [token] 用户系统的token
 11                 string param = "username=" + username + "&token=" + token;
 12                 // 设置参数
 13                 HttpWebRequest request = WebRequest.Create(serverUrl + "?" + param) as HttpWebRequest;
 14                 CookieContainer cookieContainer = new CookieContainer();
 15                 request.CookieContainer = cookieContainer;
 16                 request.AllowAutoRedirect = true;
 17                 request.Method = "POST";
 18                 string boundary = DateTime.Now.Ticks.ToString("X"); // 标识调试随机分隔线
 19                 request.ContentType = "multipart/form-data;charset=utf-8;boundary=" + boundary;
 20                 byte[] itemBoundaryBytes = Encoding.UTF8.GetBytes("\r\n--" + boundary + "\r\n");
 21                 byte[] endBoundaryBytes = Encoding.UTF8.GetBytes("\r\n--" + boundary + "--\r\n");
 22                 int pos = filepath.LastIndexOf("\\");
 23                 string fileName = filepath.Substring(pos + 1);
 24                 //请求头部信息 
 25                 StringBuilder sbHeader = new StringBuilder(string.Format("Content-Disposition:form-data;name=\"file\";filename=\"{0}\"\r\nContent-Type:application/octet-stream\r\n\r\n", fileName));
 26                 byte[] postHeaderBytes = Encoding.UTF8.GetBytes(sbHeader.ToString());
 27 
 28                 FileStream fs = new FileStream(filepath, FileMode.Open, FileAccess.Read);
 29                 byte[] bArr = new byte[fs.Length];
 30                 fs.Read(bArr, 0, bArr.Length);
 31                 fs.Close();
 32 
 33                 //把数组转换成流中所需字节数组类型
 34                 byte[] paramBytes = Encoding.UTF8.GetBytes(param);
 35                 Stream postStream = request.GetRequestStream();
 36                 postStream.Write(itemBoundaryBytes, 0, itemBoundaryBytes.Length);
 37                 postStream.Write(postHeaderBytes, 0, postHeaderBytes.Length);
 38                 postStream.Write(bArr, 0, bArr.Length);
 39                 postStream.Write(endBoundaryBytes, 0, endBoundaryBytes.Length);
 40                 postStream.Write(paramBytes, 0, paramBytes.Length);
 41                 postStream.Close();
 42                 //发送请求并获取相应回应数据
 43                 HttpWebResponse response = request.GetResponse() as HttpWebResponse;
 44                 //直到request.GetResponse()程序才开始向目标网页发送Post请求
 45                 Stream instream = response.GetResponseStream();
 46                 StreamReader sr = new StreamReader(instream, Encoding.UTF8);
 47                 //返回结果网页(html)代码
 48                 //   "state": 返回状态, "mes": 文件下载地址 
 49                 //101    转换成功
 50                 //102    转换失败
 51                 //103    转换程序异常
 52                 //104    找不到转换源文件
 53                 //105    源文件格式错误
 54                 //106    验证用户失败
 55                 //107    缺失参数
 56                 //108    服务发送数据失败
 57                 //109    上传文件异常
 58                 content = sr.ReadToEnd();
 59             }
 60             catch (Exception ex)
 61             {
 62                 content = ex.ToString();
 63             }
 64             return content;
 65         }
 66 
 67 
 68         public string HttpUploadFileofficeurl(string serverUrl, string officeurl,  string username, string token)
 69         {
 70             string content = "";
 71             try
 72             {
 73                 // [type] 操作类型转换类型(1:office转pdf、2:office转swf、
 74                 //3:office转png、4:pdf拆页、5:pdf转swf、
 75                 //6:pdf转png、7:word转txt)
 76                 // [username] 用户帐号
 77                 // [token] 用户系统的token
 78                 string param =  "username=" + username + "&token=" + token + "&officeurl=" + officeurl;
 79                 // 设置参数
 80                 HttpWebRequest request = WebRequest.Create(serverUrl + "?" + param) as HttpWebRequest;
 81                 CookieContainer cookieContainer = new CookieContainer();
 82                 request.CookieContainer = cookieContainer;
 83                 request.AllowAutoRedirect = true;
 84                 request.Method = "POST";
 85                 string boundary = DateTime.Now.Ticks.ToString("X"); // 标识调试随机分隔线
 86                 request.ContentType = "multipart/form-data;charset=utf-8;boundary=" + boundary;
 87                 byte[] itemBoundaryBytes = Encoding.UTF8.GetBytes("\r\n--" + boundary + "\r\n");
 88                 byte[] endBoundaryBytes = Encoding.UTF8.GetBytes("\r\n--" + boundary + "--\r\n");
 89                                            //请求头部信息 
 90                 StringBuilder sbHeader = new StringBuilder(string.Format("Content-Disposition:form-data;name=\"file\";\r\nContent-Type:application/octet-stream\r\n\r\n"));
 91                 byte[] postHeaderBytes = Encoding.UTF8.GetBytes(sbHeader.ToString());
 92                 //把数组转换成流中所需字节数组类型
 93                 byte[] paramBytes = Encoding.UTF8.GetBytes(param);
 94                 Stream postStream = request.GetRequestStream();
 95                 postStream.Write(itemBoundaryBytes, 0, itemBoundaryBytes.Length);
 96                 postStream.Write(postHeaderBytes, 0, postHeaderBytes.Length);
 97                 postStream.Write(endBoundaryBytes, 0, endBoundaryBytes.Length);
 98                 postStream.Write(paramBytes, 0, paramBytes.Length);
 99                 postStream.Close();
100                 //发送请求并获取相应回应数据
101                 HttpWebResponse response = request.GetResponse() as HttpWebResponse;
102                 //直到request.GetResponse()程序才开始向目标网页发送Post请求
103                 Stream instream = response.GetResponseStream();
104                 StreamReader sr = new StreamReader(instream, Encoding.UTF8);
105                 //返回结果网页(html)代码
106                 //   "state": 返回状态, "mes": 文件下载地址 
107                 //101    转换成功
108                 //102    转换失败
109                 //103    转换程序异常
110                 //104    找不到转换源文件
111                 //105    源文件格式错误
112                 //106    验证用户失败
113                 //107    缺失参数
114                 //108    服务发送数据失败
115                 //109    上传文件异常
116                 content = sr.ReadToEnd();
117             }
118             catch (Exception ex)
119             {
120                 content = ex.ToString();
121             }
122             return content;
123         }

 

转载于:https://www.cnblogs.com/edupaas/p/6015138.html

Logo

开放原子开发者工作坊旨在鼓励更多人参与开源活动,与志同道合的开发者们相互交流开发经验、分享开发心得、获取前沿技术趋势。工作坊有多种形式的开发者活动,如meetup、训练营等,主打技术交流,干货满满,真诚地邀请各位开发者共同参与!

更多推荐