winfrom中上传文件保存在webFrom里面

winfrom里面的代码

   private void button1_Click(object sender, EventArgs e)
        {
            if (!string.IsNullOrEmpty(textBox1.Text.Trim()))
            {
                string a = MyUploader(this.textBox1.Text.Trim(), @"http://localhost:63715/Default.aspx");  //这里的路径就是网页路径
                MessageBox.Show(a);
            }
            else
            {
                MessageBox.Show("请选择需要上传的文件");
            }
           
        }

        /// <summary>
        /// 文件上传
        /// </summary>
        /// <param name="strFileToUpload">文件名</param>
        /// <param name="strUrl">目标路径</param>
        /// <returns>是否成功</returns>
        public static string MyUploader(string strFileToUpload, string strUrl)
        {
            string strFileFormName = "file";
            Uri oUri = new Uri(strUrl);  //创建一个url请求
            //分隔符
            string strBoundary = "----------" + DateTime.Now.Ticks.ToString("x");
            //转成ascii码
            byte[] boundaryBytes = Encoding.ASCII.GetBytes("\r\n--" + strBoundary + "\r\n");
            //使用multipart/form-data请求体格式为(包含文件内容)
            //Content-Disposition: form-data; name="fileparam"; filename="test.txt"
           //Content-Type: text/plain

            StringBuilder sb = new StringBuilder();
            sb.Append("--");
            sb.Append(strBoundary);
            sb.Append("\r\n");
            sb.Append("Content-Disposition: form-data; name=\"");
            sb.Append(strFileFormName);
            sb.Append("\"; filename=\"");
            sb.Append(Path.GetFileName(strFileToUpload));  //文件名
            sb.Append("\"");
            sb.Append("\r\n");
            sb.Append("Content-Type: ");
            sb.Append("application/octet-stream");
            sb.Append("\r\n");
            sb.Append("\r\n");
            string strPostHeader = sb.ToString();
            //字符串转成字节
            byte[] postHeaderBytes = Encoding.UTF8.GetBytes(strPostHeader);
            //post提交  
            HttpWebRequest oWebrequest = (HttpWebRequest)WebRequest.Create(oUri);//指定url
            oWebrequest.ContentType = "multipart/form-data; boundary=" + strBoundary; //固定格式
            oWebrequest.Method = "POST";
            oWebrequest.AllowWriteStreamBuffering = false;

            FileStream oFileStream = new FileStream(strFileToUpload, FileMode.Open, FileAccess.Read);
            long length = postHeaderBytes.Length + oFileStream.Length + boundaryBytes.Length;
            oWebrequest.ContentLength = length;//确定传的长度
            Stream oRequestStream = oWebrequest.GetRequestStream();//转成流
            oRequestStream.Write(postHeaderBytes, 0, postHeaderBytes.Length);

            //checked((uint)Math.Min)
            //文件流的长度
            byte[] buffer = new Byte[checked((uint)Math.Min(4096, (int)oFileStream.Length))];
            int bytesRead = 0;
            while ((bytesRead = oFileStream.Read(buffer, 0, buffer.Length)) != 0)
                oRequestStream.Write(buffer, 0, bytesRead);
            oFileStream.Close();

            oRequestStream.Write(boundaryBytes, 0, boundaryBytes.Length);
            WebResponse oWResponse = oWebrequest.GetResponse();
            Stream s = oWResponse.GetResponseStream(); //从webfrom中返回的数据
            StreamReader sr = new StreamReader(s);
            String sReturnString = sr.ReadToEnd();
            Console.WriteLine(sReturnString);
            //用完关闭
            oFileStream.Close();
            oRequestStream.Close();
            s.Close();
            sr.Close();
            return sReturnString;
        }

 webFrom里面的代码

 protected void Page_Load(object sender, EventArgs e)
    {
        if (Request.Files.Count > 0)
        {
            try
            {
                HttpPostedFile file = Request.Files[0];  //获取传的文件
                string filePath = this.MapPath("FileUpload") + "\\" + file.FileName;
                file.SaveAs(filePath);
                Response.Write("成功了");
            }
            catch (Exception)
            {
                Response.Write("失败了1");
            }
        }
        else
        {
            Response.Write("失败了2");
        }

        Response.End();  
    }

 

posted @ 2017-03-01 21:00  Sealee  阅读(631)  评论(0编辑  收藏  举报