C#后台上传文档到服务器的方法

2016/10/31 17:55:24 www.edu-paas.com

   

C#后台上传文档到服务器的方法如下:


此方法目前在EDU直播系统中,上传授课教案中运用;同时在 文档转换、文档内容提取、上传在线文档预览、中也运用的同样方法。



protected void Page_Load(object sender, EventArgs e)
    {
        string serverUrl = "http://office.edu-paas.com/Server/FileConvert.ashx";//服务器地址
        string filepath = "C:\\Test.doc";//本地文件
        //filepath = "D:\\WebIIS\\OpenIM\\Resources\\SharesFiles\\20160529174111.docx";
        UploadFile.HttpUploadFile(serverUrl, filepath, "3");
    }

  public static string HttpUploadFile(string serverUrl, string filepath, string type)
        {
            string content = "";
            try
            {
                // [type] 操作类型
                // [username] 用户帐号
                // [token] 用户系统的token
                string param = "type=[type]&username=[username]&token=[token]"; // 设置参数
                HttpWebRequest request = WebRequest.Create(serverUrl + "?" + param) as HttpWebRequest;
                CookieContainer cookieContainer = new CookieContainer();
                request.CookieContainer = cookieContainer;
                request.AllowAutoRedirect = true;
                request.Method = "POST";
                string boundary = DateTime.Now.Ticks.ToString("X"); // 标识调试随机分隔线
                request.ContentType = "multipart/form-data;charset=utf-8;boundary=" + boundary;
                byte[] itemBoundaryBytes = Encoding.UTF8.GetBytes("\r\n--" + boundary + "\r\n");
                byte[] endBoundaryBytes = Encoding.UTF8.GetBytes("\r\n--" + boundary + "--\r\n");
                int pos = filepath.LastIndexOf("\\");
                string fileName = filepath.Substring(pos + 1);
                //请求头部信息 
                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));
                byte[] postHeaderBytes = Encoding.UTF8.GetBytes(sbHeader.ToString());
                FileStream fs = new FileStream(filepath, FileMode.Open, FileAccess.Read);
                byte[] bArr = new byte[fs.Length];
                fs.Read(bArr, 0, bArr.Length);
                fs.Close();
                //把数组转换成流中所需字节数组类型
                byte[] paramBytes = Encoding.UTF8.GetBytes(param);
                Stream postStream = request.GetRequestStream();
                postStream.Write(itemBoundaryBytes, 0, itemBoundaryBytes.Length);
                postStream.Write(postHeaderBytes, 0, postHeaderBytes.Length);
                postStream.Write(bArr, 0, bArr.Length);
                postStream.Write(endBoundaryBytes, 0, endBoundaryBytes.Length);
                postStream.Write(paramBytes, 0, paramBytes.Length);
                postStream.Close();
                //发送请求并获取相应回应数据
                HttpWebResponse response = request.GetResponse() as HttpWebResponse;
                //直到request.GetResponse()程序才开始向目标网页发送Post请求
                Stream instream = response.GetResponseStream();
                StreamReader sr = new StreamReader(instream, Encoding.UTF8);
                //返回结果网页(html)代码
                content = sr.ReadToEnd();
            }
            catch (Exception ex)
            {
                content = ex.ToString();
            }
            return content;
        }



大家互相学习,共同努力,加油!


各位多多提意见,先在这里谢谢哈,也希望一切顺心!