C#中用WebClient.UploadData 方法上载文件数据

假如某网站有个表单,例如(url: http://localhost/login.aspx):
帐号
密码

我们需要在程序中提交数据到这个表单,对于这种表单,我们可以使用 WebClient.UploadData 方法来实现,将所要上传的数据拼成字符即可,程序很简单:
string uriString = "http://localhost/login.aspx"; // 创建一个新的 WebClient 实例. WebClient myWebClient = new WebClient(); string postData = "Username=admin&Password=admin"; // 注意这种拼字符串的ContentType myWebClient.Headers.Add("Content-Type","application/x-www-form-urlencoded"); // 转化成二进制数组 byte[] byteArray = Encoding.ASCII.GetBytes(postData); // 上传数据,并获取返回的二进制数据. byte[] responseArray = myWebClient.UploadData(uriString,"POST",byteArray);

对于文件上传类的表单,例如(url: http://localhost/uploadFile.aspx):
文件

对于这种表单,我们可以使用
String uriString = "http://localhost/uploadFile.aspx"; // 创建一个新的 WebClient 实例. WebClient myWebClient = new WebClient(); string fileName = @"C:\upload.txt"; // 直接上传,并获取返回的二进制数据. byte[] responseArray = myWebClient.UploadFile(uriString,"POST",fileName);

还有一种表单,不仅有文字,还有文件,例如(url: http://localhost/uploadData.aspx):
文件名
文件

对于这种表单,似乎前面的两种方法都不能适用,对于第一种方法,不能直接拼字符串,对于第二种,我们只能传文件,重新回到第一个方法,注意参数:
public byte[] UploadData(
   string address,
   string method,
   byte[] data
);
在第一个例子中,是通过拼字符串来得到byte[] data参数值的,对于这种表单显然不行,反过来想想,对于uploadData.aspx这样的程序来说,直接通过网页提交数据,后台所获取到的流是什么样的呢?(在我以前的一篇blog中,曾分析过这个问题:asp无组件上传进度条解决方案),最终的数据如下:
-----------------------------7d429871607fe
Content-Disposition: form-data; name="file1"; filename="G:\homepage.txt"
Content-Type: text/plain
宝玉:http://www.webuc.net
-----------------------------7d429871607fe
Content-Disposition: form-data; name="filename"
default filename
-----------------------------7d429871607fe--


所以只要拼一个这样的byte[] data数据Post过去,就可以达到同样的效果了。但是一定要注意,对于这种带有文件上传的,其ContentType是不一样的,例如上面的这种,其ContentType为"multipart/form-data; boundary=---------------------------7d429871607fe"。有了ContentType,我们就可以知道boundary(就是上面的"---------------------------7d429871607fe"),知道boundary了我们就可以构造出我们所需要的byte[] data了,最后,不要忘记,把我们构造的ContentType传到WebClient中(例如:webClient.Headers.Add("Content-Type", ContentType);)这样,就可以通过WebClient.UploadData 方法上载文件数据了。

具体代码如下:
生成二进制数据类的封装
using System;
using System.Web;
using System.IO;
using System.Net;
using System.Text;
using System.Collections;

namespace UploadData.Common
...{
    /** <summary>
    /// 创建WebClient.UploadData方法所需二进制数组
    /// </summary>
    public class CreateBytes
    ...{
        Encoding encoding = Encoding.UTF8;

        /** <summary>
        /// 拼接所有的二进制数组为一个数组
        /// </summary>
        /// <param name="byteArrays">数组</param>
        /// <returns></returns>
        /// <remarks>加上结束边界</remarks>
        public byte[] JoinBytes(ArrayList byteArrays)
        ...{
            int length = 0;
            int readLength = 0;

            // 加上结束边界
            string endBoundary = Boundary + "--\r\n"; //结束边界
            byte[] endBoundaryBytes = encoding.GetBytes(endBoundary);
            byteArrays.Add(endBoundaryBytes);

            foreach(byte[] b in byteArrays)
            ...{
                length += b.Length;
            }
            byte[] bytes = new byte[length];

            // 遍历复制
            //
            foreach(byte[] b in byteArrays)
            ...{
                b.CopyTo(bytes, readLength);
                readLength += b.Length;
            }

            return bytes;
        }

        public bool UploadData(string uploadUrl, byte[] bytes, out byte[] responseBytes)
        ...{
            WebClient webClient = new WebClient();
            webClient.Headers.Add("Content-Type", ContentType);

            try
            ...{
                responseBytes = webClient.UploadData(uploadUrl, bytes);
                return true;
            }
            catch (WebException ex)
            ...{
                Stream resp = ex.Response.GetResponseStream();
                responseBytes = new byte[ex.Response.ContentLength];
                resp.Read(responseBytes, 0, responseBytes.Length);               
            }
            return false;
        }

 

        /** <summary>
        /// 获取普通表单区域二进制数组
        /// </summary>
        /// <param name="fieldName">表单名</param>
        /// <param name="fieldValue">表单值</param>
        /// <returns></returns>
        /// <remarks>
        /// -----------------------------7d52ee27210a3c\r\nContent-Disposition: form-data; name=\"表单名\"\r\n\r\n表单值\r\n
        /// </remarks>
        public byte[] CreateFieldData(string fieldName, string fieldValue)
        ...{
            string textTemplate = Boundary + "\r\nContent-Disposition: form-data; name=\"{0}\"\r\n\r\n{1}\r\n";
            string text = String.Format(textTemplate, fieldName, fieldValue);
            byte[] bytes = encoding.GetBytes(text);
            return bytes;
        }

       
        /** <summary>
        /// 获取文件上传表单区域二进制数组
        /// </summary>
        /// <param name="fieldName">表单名</param>
        /// <param name="filename">文件名</param>
        /// <param name="contentType">文件类型</param>
        /// <param name="contentLength">文件长度</param>
        /// <param name="stream">文件流</param>
        /// <returns>二进制数组</returns>
        public byte[] CreateFieldData(string fieldName, string filename,string contentType, byte[] fileBytes)
        ...{
            string end = "\r\n";
            string textTemplate = Boundary + "\r\nContent-Disposition: form-data; name=\"{0}\"; filename=\"{1}\"\r\nContent-Type: {2}\r\n\r\n";
           
            // 头数据
            string data = String.Format(textTemplate, fieldName, filename, contentType);
            byte[] bytes = encoding.GetBytes(data);

           

            // 尾数据
            byte[] endBytes = encoding.GetBytes(end);

            // 合成后的数组
            byte[] fieldData = new byte[bytes.Length + fileBytes.Length + endBytes.Length];

            bytes.CopyTo(fieldData, 0); // 头数据
            fileBytes.CopyTo(fieldData, bytes.Length); // 文件的二进制数据
            endBytes.CopyTo(fieldData, bytes.Length + fileBytes.Length); // \r\n

            return fieldData;
        }


        属性#region 属性
        public string Boundary
        ...{
            get
            ...{
                string[] bArray, ctArray;
                string contentType = ContentType;
                ctArray = contentType.Split(';');
                if (ctArray[0].Trim().ToLower() == "multipart/form-data")
                ...{
                    bArray = ctArray[1].Split('=');
                    return "--" + bArray[1];
                }
                return null;
            }
        }

        public string ContentType
        ...{
            get ...{
                if (HttpContext.Current == null)
                ...{
                    return "multipart/form-data; boundary=---------------------------7d5b915500cee";
                }
                return HttpContext.Current.Request.ContentType;
            }
        }
        #endregion
    }
}

在Winform中调用


using System;
using System.Drawing;
using System.Collections;
using System.ComponentModel;
using System.Windows.Forms;
using System.Data;

using UploadData.Common;
using System.IO;

namespace UploadDataWin
...{
    /** <summary>
    /// frmUpload 的摘要说明。
    /// </summary>
    public class frmUpload : System.Windows.Forms.Form
    ...{
        private System.Windows.Forms.Label lblAmigoToken;
        private System.Windows.Forms.TextBox txtAmigoToken;
        private System.Windows.Forms.Label lblFilename;
        private System.Windows.Forms.TextBox txtFilename;
        private System.Windows.Forms.Button btnBrowse;
        private System.Windows.Forms.TextBox txtFileData;
        private System.Windows.Forms.Label lblFileData;
        private System.Windows.Forms.Button btnUpload;
        private System.Windows.Forms.OpenFileDialog openFileDialog1;
        private System.Windows.Forms.TextBox txtResponse;
        /** <summary>
        /// 必需的设计器变量。
        /// </summary>
        private System.ComponentModel.Container components = null;

        public frmUpload()
        ...{
            //
            // Windows 窗体设计器支持所必需的
            //
            InitializeComponent();

            //
            // TODO: 在 InitializeComponent 调用后添加任何构造函数代码
            //
        }

        /** <summary>
        /// 清理所有正在使用的资源。
        /// </summary>
        protected override void Dispose( bool disposing )
        ...{
            if( disposing )
            ...{
                if (components != null)
                ...{
                    components.Dispose();
                }
            }
            base.Dispose( disposing );
        }

        Windows 窗体设计器生成的代码#region Windows 窗体设计器生成的代码
        /** <summary>
        /// 设计器支持所需的方法 - 不要使用代码编辑器修改
        /// 此方法的内容。
        /// </summary>
        private void InitializeComponent()
        ...{
            this.lblAmigoToken = new System.Windows.Forms.Label();
            this.txtAmigoToken = new System.Windows.Forms.TextBox();
            this.lblFilename = new System.Windows.Forms.Label();
            this.txtFilename = new System.Windows.Forms.TextBox();
            this.btnBrowse = new System.Windows.Forms.Button();
            this.txtFileData = new System.Windows.Forms.TextBox();
            this.lblFileData = new System.Windows.Forms.Label();
            this.btnUpload = new System.Windows.Forms.Button();
            this.openFileDialog1 = new System.Windows.Forms.OpenFileDialog();
            this.txtResponse = new System.Windows.Forms.TextBox();
            this.SuspendLayout();
            //
            // lblAmigoToken
            //
            this.lblAmigoToken.Location = new System.Drawing.Point(40, 48);
            this.lblAmigoToken.Name = "lblAmigoToken";
            this.lblAmigoToken.Size = new System.Drawing.Size(72, 23);
            this.lblAmigoToken.TabIndex = 0;
            this.lblAmigoToken.Text = "AmigoToken";
            //
            // txtAmigoToken
            //
            this.txtAmigoToken.Location = new System.Drawing.Point(120, 48);
            this.txtAmigoToken.Name = "txtAmigoToken";
            this.txtAmigoToken.Size = new System.Drawing.Size(248, 21);
            this.txtAmigoToken.TabIndex = 1;
            this.txtAmigoToken.Text = "";
            //
            // lblFilename
            //
            this.lblFilename.Location = new System.Drawing.Point(40, 96);
            this.lblFilename.Name = "lblFilename";
            this.lblFilename.Size = new System.Drawing.Size(80, 23);
            this.lblFilename.TabIndex = 2;
            this.lblFilename.Text = "Filename";
            //
            // txtFilename
            //
            this.txtFilename.Location = new System.Drawing.Point(120, 96);
            this.txtFilename.Name = "txtFilename";
            this.txtFilename.Size = new System.Drawing.Size(248, 21);
            this.txtFilename.TabIndex = 3;
            this.txtFilename.Text = "";
            //
            // btnBrowse
            //
            this.btnBrowse.Location = new System.Drawing.Point(296, 144);
            this.btnBrowse.Name = "btnBrowse";
            this.btnBrowse.TabIndex = 4;
            this.btnBrowse.Text = "浏览...";
            this.btnBrowse.Click += new System.EventHandler(this.btnBrowse_Click);
            //
            // txtFileData
            //
            this.txtFileData.Location = new System.Drawing.Point(120, 144);
            this.txtFileData.Name = "txtFileData";
            this.txtFileData.Size = new System.Drawing.Size(168, 21);
            this.txtFileData.TabIndex = 5;
            this.txtFileData.Text = "";
            //
            // lblFileData
            //
            this.lblFileData.Location = new System.Drawing.Point(40, 144);
            this.lblFileData.Name = "lblFileData";
            this.lblFileData.Size = new System.Drawing.Size(72, 23);
            this.lblFileData.TabIndex = 6;
            this.lblFileData.Text = "FileData";
            //
            // btnUpload
            //
            this.btnUpload.Location = new System.Drawing.Point(48, 184);
            this.btnUpload.Name = "btnUpload";
            this.btnUpload.TabIndex = 7;
            this.btnUpload.Text = "Upload";
            this.btnUpload.Click += new System.EventHandler(this.btnUpload_Click);
            //
            // txtResponse
            //
            this.txtResponse.Location = new System.Drawing.Point(136, 184);
            this.txtResponse.Multiline = true;
            this.txtResponse.Name = "txtResponse";
            this.txtResponse.Size = new System.Drawing.Size(248, 72);
            this.txtResponse.TabIndex = 8;
            this.txtResponse.Text = "";
            //
            // frmUpload
            //
            this.AutoScaleBaseSize = new System.Drawing.Size(6, 14);
            this.ClientSize = new System.Drawing.Size(400, 269);
            this.Controls.Add(this.txtResponse);
            this.Controls.Add(this.btnUpload);
            this.Controls.Add(this.lblFileData);
            this.Controls.Add(this.txtFileData);
            this.Controls.Add(this.btnBrowse);
            this.Controls.Add(this.txtFilename);
            this.Controls.Add(this.lblFilename);
            this.Controls.Add(this.txtAmigoToken);
            this.Controls.Add(this.lblAmigoToken);
            this.Name = "frmUpload";
            this.Text = "frmUpload";
            this.ResumeLayout(false);

        }
        #endregion

        /** <summary>
        /// 应用程序的主入口点。
        /// </summary>
        [STAThread]
        static void Main()
        ...{
            Application.Run(new frmUpload());
        }

        private void btnUpload_Click(object sender, System.EventArgs e)
        ...{
            // 非空检验
            if (txtAmigoToken.Text.Trim() == "" || txtFilename.Text == "" || txtFileData.Text.Trim() == "")
            ...{
                MessageBox.Show("Please fill data");
                return;
            }

            // 所要上传的文件路径
            string path = txtFileData.Text.Trim();

            // 检查文件是否存在
            if (!File.Exists(path))
            ...{
                MessageBox.Show("{0} does not exist!", path);
                return;
            }

            // 读文件流
            FileStream fs = new FileStream(path, FileMode.Open,
                FileAccess.Read, FileShare.Read);
           
            // 这部分需要完善
            string ContentType = "application/octet-stream";
            byte[] fileBytes = new byte[fs.Length];
            fs.Read(fileBytes, 0, Convert.ToInt32(fs.Length));


            // 生成需要上传的二进制数组
            CreateBytes cb = new CreateBytes();
            // 所有表单数据
            ArrayList bytesArray = new ArrayList();
            // 普通表单
            bytesArray.Add(cb.CreateFieldData("FileName", txtFilename.Text));
            bytesArray.Add(cb.CreateFieldData("AmigoToken", txtAmigoToken.Text));
            // 文件表单
            bytesArray.Add(cb.CreateFieldData("FileData", path
                                                , ContentType, fileBytes));

            // 合成所有表单并生成二进制数组
            byte[] bytes = cb.JoinBytes(bytesArray);
           
            // 返回的内容
            byte[] responseBytes;
           
            // 上传到指定Url
            bool uploaded = cb.UploadData("http://localhost/UploadData/UploadAvatar.aspx", bytes, out responseBytes);

            // 将返回的内容输出到文件
            using (FileStream file = new FileStream(@"c:\response.text", FileMode.Create, FileAccess.Write, FileShare.Read))
            ...{
                file.Write(responseBytes, 0, responseBytes.Length);
            }

            txtResponse.Text = System.Text.Encoding.UTF8.GetString(responseBytes);

        }

        private void btnBrowse_Click(object sender, System.EventArgs e)
        ...{
            if(openFileDialog1.ShowDialog() == DialogResult.OK)
            ...{
                txtFileData.Text = openFileDialog1.FileName;
            }

        }
    }
}

转载于:https://www.cnblogs.com/MaxWoods/archive/2009/12/23/1630921.html

本文来自互联网用户投稿,该文观点仅代表作者本人,不代表本站立场。本站仅提供信息存储空间服务,不拥有所有权,不承担相关法律责任。如若转载,请注明出处:http://www.mzph.cn/news/363411.shtml

如若内容造成侵权/违法违规/事实不符,请联系多彩编程网进行投诉反馈email:809451989@qq.com,一经查实,立即删除!

相关文章

Java中的SynchronousQueue示例–生产者使用者解决方案

SynchronousQueue是BlockingQueue的一种特殊类型&#xff0c;其中每个插入操作必须等待另一个线程进行相应的删除操作&#xff0c;反之亦然。 当您在SynchronousQueue上调用put&#xff08;&#xff09;方法时&#xff0c;它将阻塞&#xff0c;直到有另一个线程将该元素从Queue…

OnSen UI结合AngularJs打造”美团APP我的”页面 --Hybrid App

1、页面效果图&#xff1a; 演示地址&#xff1a;http://www.nxl123.cn/bokeyuan/meiTuanDemo_mine/ 2、核心代码 mine.html&#xff1a; <ons-page id"mine" ng-controller"MineController"> <!--toolbar开始--> <ons-toolbar>…

[MOSS开发]:通过简单BUG跟踪Demo阐述用户控件对列表的操作

下面的文章我想以一个具体的BUG跟踪Demo来说明MOSS的具体应用,这里面会应用到下面的知识点: 1:用户组,用户的创建,权限分配&#xff1b; 2:列表的概念以及创建&#xff1b; 3:利用用户控件来完成表单的增加加功能&#xff1b; 4:当前域用户查看自己BUG。 BUG跟踪软件在一些…

Maven的课堂笔记4

9.Maven与MyEclipse2014结合 MyEclipse10以上的版本,对Maven支持的就比较好 9.2 Myeclipse配置 本地文件夹的C盘的.m2文件夹下必须得有这个settings.xml文件 不配置这个settings.xml文件的话,myeclipse会从互联网上下载需要的jar包. 9.3 修改pom文件 添加jar包 <project xml…

vue动画

vue 提供了一些显示、隐藏一些不同的过渡&#xff0c;效果主要跟 v-if v-show 动态组件 1. vue 给动画分了 6 个过程&#xff0c;在 css 中扮演 6 个类 .v-enter  定义动画的开始状态 .v-enter-active  定义动画生效时的状态 .v-enter-to  定义动画结束是的状态 .v-leave…

基于AngularJS的Onsen UI --Onsen UI学习笔记

AngularJS与Onsen UI的结合&#xff0c;Onsen UI应用程序实际上是一个AngularJS 1应用程序。 <!doctype html><html lang"en"><head> <meta charset"utf-8"> <link rel"stylesheet" href"lib/onsen/css/…

图数据库的知识表示与推理

图形数据库及其技术生态系统可以为知识表示和推理问题提供优雅&#xff0c;有效的解决方案。 要了解这种说法&#xff0c;我们必须首先了解什么是图形。 图是一种数据结构。 图数据结构的类型很多&#xff0c;但出于本文的目的&#xff0c;我们将重点介绍一种已被称为属性图的类…

解决win2003安装exchangeServer后关机慢的方法

在windows2003上安装exchange2003&#xff08;或者是exchange2000&#xff09;后&#xff0c;很多用户发现服务器关闭变得非常之慢……很不幸的是&#xff0c;我也遇到了这个问题。从日志中的错误信息来分析&#xff0c;和目录服务先于某些服务终止有关。在微软的KB上查了一下&…

vegas 为盖斯

vegas 为盖斯 S键 分割素材U键 分开视频和音频I键渲染开始O渲染结束 默认布局 为盖斯新建项目的参数 剪好后渲染 插入字幕 转载于:https://www.cnblogs.com/GaoNa/p/10562504.html

简单配置 docker swarm

简单配置 docker swarm #准备三台CentOS7 #IP划分 192.168.1.201 virtualBox1 192168.1.202 virtualBox2 192168.1.204 virtualBox3 #三台机器上分别安装docker docker安装 #三台机器上分别配置加速器 vim /etc/docker/daemon.json #该文…

css图片的全屏显示代码-css3

<!DOCTYPE html><html lang"en">  <head>     <meta charset"UTF-8">     <title>Title</title>     <style type"text/css" rel"stylesheet">       *{margin: 0;pad…

COMET彗星(三)构建自己的COMET核心

主题列表&#xff1a; COMET彗星&#xff08;一&#xff09;SERVER PUSH介绍 COMET彗星&#xff08;二&#xff09;基于SERVER PUSH的消息传输 引言&#xff1a; 在上一篇随笔中&#xff0c;对COMET使用的类和作用进行了简短的介绍&#xff0c;从本篇随笔开始&#xff0c;将从实…

如何在JavaServer Pages中使用Salesforce REST API

摘要&#xff1a;本教程提供了一个JSP示例以及如何将其与Salesforce REST API集成。 我们将逐步完成创建外部客户端以使用Force.com &#xff08;同时使用HTTP&#xff08;S&#xff09;和JSON&#xff09;管理您的数据的分步过程。 在此示例中&#xff0c;我将Mac OS X 10.9.…

写在08年“愚人节”

从博客园学了很多东西&#xff0c;是时候该回馈了。谢谢博客园给我们提供这么好的平台交流技术。 刚发了文章习惯性的看了下自己博客的首页代码&#xff0c;感觉日历不够精良。 我做过的一个ajax日历&#xff0c;可以参考解放日报 艺术家具版的日期直达功能的日历&#xff0c;用…

19.3.20 解决pycharm快捷键无法使用问题和熟悉git与码云操作流程

problem&#xff1a;由于Vim插件导致快捷键无法使用&#xff1b; answer&#xff1a;settings→Plugins→搜索到ideaVim→取消选中→apply→重启pycharm&#xff1b; git&#xff1a;创建仓库→生成公钥&#xff08;ssh-keygen -t rsa -C "962891994qq.com"&#xff…

JQuery实现点击按钮切换图片(附源码)--JQuery基础

JQuery实现切换图片相对比较简单&#xff0c;直接贴代码了哈&#xff0c;有注释噢&#xff01;疑问请追加评论哈&#xff0c;不足之处还请大佬们指出&#xff01; 1、案例代码&#xff1a; demo.html&#xff1a; <!DOCTYPE html><html><head>   <me…

Java EE陷阱#1:忽略@Singleton的默认锁定

EJB Singleton Bean是EJB 3.1规范引入的&#xff0c;通常用于存储缓存的数据。 这意味着&#xff0c;我们尝试通过使用Singleton来提高应用程序的性能。 总的来说&#xff0c;这很好。 特别是在并行调用不多的情况下。 但是&#xff0c;如果我们忽略默认锁&#xff0c;并且并行…

js左右对联

相当着急的找了一早上&#xff0c;幸好有原来的代码作为基础&#xff0c;不害怕。<script>function initEcAd() {document.all.AdLayer1.style.posTop -200;document.all.AdLayer1.style.visibility visibledocument.all.AdLayer2.style.posTop -200;document.all.AdL…

cocos2d-x 横屏、竖屏(以及基于传感器)、读写sd卡

一、横屏、竖屏设置&#xff08;以及基于传感器的&#xff09; AndroidManifest.xml文件中&#xff0c; 1、不论任何情况的&#xff1a; screenOrientation"landscape" 为横屏 screenOrientation"portrait"为竖屏 …

CSS3盒子模型

web前端必须了解的CSS3盒子模型 1、需要了解的属性以及属性值 display:box或者display:inline-box box-orient:horizontal | vertical (水平 垂直) 定义盒模型的布局方向 box-direction:normal reverse(正序 反序) 元素排列顺序 box-ordinal-group:number(数值) 设置元素…