POST上传

来源:互联网 发布:大数据行业前景 编辑:程序博客网 时间:2024/05/17 04:27

private Boolean UploadFile()
    {
        Boolean flag = false;
        try
        {
            foreach (string f in Request.Files.AllKeys)
            {

                HttpPostedFile postFile = Request.Files[f];
                if (postFile.ContentLength > 10)
                {
                    #region get new filename
                    string txtFilePath = postFile.FileName;                             //file path                   
                    string fileExtName = txtFilePath.Substring(txtFilePath.LastIndexOf(".") + 1, 3);  //file expand name
                    string fileName = txtFilePath.Substring(txtFilePath.LastIndexOf("//") + 1);   //file name
                    //string patha = Server.MapPath("../fax");
                    string patha = @"......";
                    string strname = "fax" + System.DateTime.Now.ToString("yyyyMMddhhmmssff") + "." + fileExtName;
                    string lstrFileNamePath = patha + @"/" + strname;
                    uploadfilename = strname;
                    #endregion
                   
                    postFile.SaveAs(lstrFileNamePath);                   
                    HttpWebRequest httprequest = (HttpWebRequest)System.Net.WebRequest.Create("url");
                    httprequest.Method = "POST";
                    httprequest.KeepAlive = false;
                    httprequest.ContentType = "multipart/form-data";
                    httprequest.Timeout = 10 * 1000;
                    httprequest.ContentLength = postFile.ContentLength;
                    Stream sr = httprequest.GetRequestStream();
                    using (FileStream fs = File.OpenRead(lstrFileNamePath))
                    {
                        byte[] b = new byte[postFile.ContentLength];
                        UTF8Encoding temp = new UTF8Encoding(true);
                        while (fs.Read(b, 0, postFile.ContentLength) > 0)
                        {
                            sr.Write(b, 0, postFile.ContentLength);
                        }
                    }


                    HttpWebResponse respon = (HttpWebResponse)httprequest.GetResponse();
                    if (httprequest.HaveResponse)
                    {
                        Response.Write("向另外一个服务器post成功:" + lstrFileNamePath);
                        HttpWebResponse rs = (HttpWebResponse)httprequest.GetResponse();
                        Stream rstream = respon.GetResponseStream();
                        StreamReader rstreamread = new StreamReader(rstream);
                        Response.Write(rstreamread.ReadToEnd());
                        flag = true;
                        break;
                    }
                    else
                    {
                        Response.Write("向另外一个服务器post失败");
                    }
                    sr.Close();
                }

            }
            return flag;
        }
        catch (Exception e)
        {
            Response.Write("失败:"+e.ToString());
            return false;
        }
    }

原创粉丝点击