zoukankan      html  css  js  c++  java
  • winform采用POST上传指定文件,并获取返回值

          由于最近有个项目需要批量上传到服务器,并以一定的规则过滤上传数据。故做了个C#小程序来实现。该方法也是借鉴了网上的方法,并精简了代码。废话少说,来看代码。

          Web端代码

    代码
     1 public partial class transform : System.Web.UI.Page
     2 {
     3     string path = "E:\\test\\uploadfi";
     4     protected void Page_Load(object sender, EventArgs e)
     5     {
     6       
     7         if (!string.IsNullOrEmpty(Request["fileName"]))
     8         {
     9            Response.Write(UploadFileWithString(Request["fileName"], Request.InputStream));
    10             //Response输出返回值
    11             
    12         }
    13     }
    14 
    15 
    16     protected string UploadFileWithString(string fileName,Stream streams)
    17     {
    18         
    19          byte[] b = new byte[Convert.ToInt32( streams.Length)];
    20          streams.Read(b, 0, Convert.ToInt32(streams.Length));
    21         
    22         string creatpath = path + "\\sh";
    23         MemoryStream ms = new MemoryStream(b);          
    24          if (!Directory.Exists(creatpath))
    25             Directory.CreateDirectory(creatpath);
    26         try
    27         {
    28             FileStream fs = new FileStream(creatpath + "\\" + fileName, FileMode.Create);
    29             ms.WriteTo(fs);
    30             ms.Close();
    31             fs.Close();
    32             return "1";
    33         }
    34         catch
    35         {
    36             return "0";
    37         }
    38     }

          winform端代码

        

    代码
     1  private void button2_Click(object sender, EventArgs e)
     2         {
     3             
     4             if (openFileDialog1.ShowDialog() == DialogResult.OK)
     5                 label2.Text = openFileDialog1.FileName;
     6             FileInfo f = new FileInfo(openFileDialog1.FileName);
     7             WebUpload(openFileDialog1.SafeFileName, f);
     8          
     9         }
    10  protected void WebUpload(string fileName, FileInfo f)
    11         {
    12             WebClient webc = new WebClient();
    13             FileStream fs = new FileStream(f.FullName, FileMode.Open, FileAccess.Read);
    14             byte[] byteString=new byte[f.Length];
    15             fs.Read(byteString, 0,Convert.ToInt32(f.Length));
    16             fs.Close();
    17             
    18             byte[] returnVal = webc.UploadData("http://localhost/sz/transform.aspx?fileName="+HttpUtility.UrlEncode(fileName,Encoding.GetEncoding("gb2312")), "post",byteString);
    19             
    20             MessageBox.Show(Encoding.GetEncoding("gb2312").GetString(returnVal));//返回值
    21             
    22         }

        

         

  • 相关阅读:
    团队工作第四次推进之——软件设计规格说明书
    失物找寻APP软件需求规格说明书——第三次团队作业
    你还在为校园内丢失东西无处可寻而发愁吗?速戳进来
    十分有趣却有些遗憾的结对编程——两位女程序员的挣扎
    结对编程初涉猎——结对伙伴的代码复审
    个人实战演练全过程——No.1 最大连续子数组求和
    小白出品 单元测试相关——入门级说明书
    写着写着停不下来的普通女程序员的总结
    vs2010 和vs2012的区别 副标题--Loaded事件走两次
    汽车防撞软件引发的一套软件系统思路
  • 原文地址:https://www.cnblogs.com/wishbay/p/1954941.html
Copyright © 2011-2022 走看看