關於HttpWebRequest上傳文件


我們web 操作離不開 http請求響應 HttpWebRequest上傳文件也是一樣的道理

下面碼一些代碼:

  

 private void UploadFile(string strRequestUri, string strCookie, string filename)
    {
        // 初始化HttpWebRequest
        HttpWebRequest httpRequest = (HttpWebRequest)HttpWebRequest.Create(strRequestUri);

        // 封裝Cookie
        Uri uri = new Uri(strRequestUri);
        Cookie cookie = new Cookie("Name", strCookie); 
        CookieContainer cookies = new CookieContainer();
        cookies.Add(uri, cookie);
        httpRequest.CookieContainer = cookies;

        // 生成時間戳
        string strBoundary = "----------" + DateTime.Now.Ticks.ToString("x");
        byte[] boundaryBytes = Encoding.ASCII.GetBytes(string.Format("\r\n--{0}--\r\n", strBoundary));

        // 填報文類型
        httpRequest.Method = "Post";
        httpRequest.Timeout = 1000 * 120;
        httpRequest.ContentType = "multipart/form-data; boundary=" + strBoundary;

        // 封裝HTTP報文頭的流
        StringBuilder sb = new StringBuilder();
        sb.Append("--");
        sb.Append(strBoundary);
        sb.Append(Environment.NewLine);
        sb.Append("Content-Disposition: form-data; name=\"");
        sb.Append("file");
        sb.Append("\"; filename=\"");
        sb.Append(filename);
        sb.Append("\"");
        sb.Append(Environment.NewLine);
        sb.Append("Content-Type: ");
        sb.Append("multipart/form-data;");
        sb.Append(Environment.NewLine);
        sb.Append(Environment.NewLine);
        byte[] postHeaderBytes = Encoding.UTF8.GetBytes(sb.ToString());

        // 計算報文長度
        long length = postHeaderBytes.Length + this.FileUpload1.PostedFile.InputStream.Length + boundaryBytes.Length;
        httpRequest.ContentLength = length;

        // 將報文頭寫入流
        Stream requestStream = httpRequest.GetRequestStream();
        requestStream.Write(postHeaderBytes, 0, postHeaderBytes.Length);

        // 將上傳文件內容寫入流 //每次上傳4k  1024*4 
        byte[] buffer = new Byte[checked((uint)Math.Min(4096, (int)this.FileUpload1.PostedFile.InputStream.Length))];
        int bytesRead = 0;

        while ((bytesRead = this.FileUpload1.PostedFile.InputStream.Read(buffer, 0, buffer.Length)) != 0)
        {
            requestStream.Write(buffer, 0, bytesRead);
        }

        // 將報文尾部寫入流
        requestStream.Write(boundaryBytes, 0, boundaryBytes.Length);
        // 關閉流
        requestStream.Close();
    }


    protected void btnRelease_Click(object sender, EventArgs e)
    {
        this.UploadFile(@"http://localhost/Qpdfgesigntest/SystemManager/WebsitePublishing/test.aspx", "yangtest", this.FileUpload1.PostedFile.FileName);
    }
public partial class SystemManager_WebsitePublishing_test : System.Web.UI.Page
{
    protected void Page_Load(object sender, EventArgs e)
    {
        if (Request.Files.Count > 0)
        {
            try
            {
                HttpPostedFile file = Request.Files[0];
                string filePath = "D:\\test\\" + file.FileName;
                file.SaveAs(filePath);
               
            }
            catch
            {
                
            }
        }
    }
}

 


免責聲明!

本站轉載的文章為個人學習借鑒使用,本站對版權不負任何法律責任。如果侵犯了您的隱私權益,請聯系本站郵箱yoyou2525@163.com刪除。



 
粵ICP備18138465號   © 2018-2025 CODEPRJ.COM