zoukankan      html  css  js  c++  java
  • 使用System.Net.WebClient上传文件

    由于一直从事Web方面的开发工作,所以对Winform这块不太熟悉,今天接到一个新的需求,就是在一个C++程序里面需要上传一个文件到IIS服务器上面来,由于C++的C#的差异性,我们不能直接使用对象,所以我们决定采用字节流的方式来上传文件。

    由于只是一个测试程序,而且C++那边还没有开发完,所以我就贴了我C#这边的一段测试程序,原理是一样的。

    发送端代码,这里代码相对简陋,大家看看就可以了,需要的话可以自己优化

            private void button1_Click(object sender, EventArgs e)
            {
                System.Net.WebClient client = new System.Net.WebClient();
    
                client.UploadFile("http://192.168.1.150:12236/default.aspx?filename=1.3.7.42.rar", "d:/1.3.7.42.rar");
            }

    然后就是接受端的代码了。也是很简单的

    复制代码
            protected void Page_Load(object sender, EventArgs e)
            {
                string fileName = "c:/vhost/wenjianshangchuan/" + Request.QueryString["filename"].Replace(".rar", "_bak.rar");
                System.IO.Stream stream = Request.InputStream;
                byte[] bytes = new byte[stream.Length];
                stream.Read(bytes, 0, bytes.Length);
                stream.Seek(0, SeekOrigin.Begin);
                stream.Flush();
                stream.Close();
                stream.Dispose();
    
                FileStream fs = new FileStream(fileName, FileMode.Create);
                BinaryWriter bw = new BinaryWriter(fs);
                bw.Write(bytes);
                bw.Close();
                fs.Close();
            }
    复制代码
    我是一只辛勤耕耘的蚂蚁
    分类: .net
  • 相关阅读:
    HDU 5033 Building --离线+单调栈
    HDU 5025 Saving Tang Monk --BFS
    Codeforces Round #267 Div.2 D Fedor and Essay -- 强连通 DFS
    Codeforces Round #267 Div2 C George and Job --DP
    POJ 3150 Cellular Automaton --矩阵快速幂及优化
    TopCoder SRM 633 Div.2 500 Jumping
    HDU 4998 Rotate --几何
    一些语言方面的技巧
    HDU 5017 Ellipsoid 模拟退火第一题
    HDU 5015 233 Matrix --矩阵快速幂
  • 原文地址:https://www.cnblogs.com/Leo_wl/p/2734500.html
Copyright © 2011-2022 走看看