1、HTTP上传文件及传递参数

    #region 6.0 上传多个文件和参数
/// <summary>
/// HttpUploadFile
/// </summary>
/// <param name="url"></param>
/// <param name="files"></param>
/// <param name="data"></param>
/// <param name="encoding"></param>
/// <returns></returns>
public static string HttpUploadFile(string url, string[] files, Dictionary<string,string> data, Encoding encoding)
{
//必须
string boundary = DateTime.Now.Ticks.ToString("X");
//必须的 //form 开始标志
byte[] boundarybytes = Encoding.ASCII.GetBytes("\r\n--" + boundary + "\r\n"); //form 结尾标志
byte[] endbytes = Encoding.ASCII.GetBytes("\r\n--" + boundary + "--\r\n"); //1.HttpWebRequest
HttpWebRequest request = (HttpWebRequest)WebRequest.Create(url); request.ContentType = "multipart/form-data;charset=utf-8;boundary=" + boundary;
request.Method = "POST";
request.KeepAlive = true;
request.Credentials = CredentialCache.DefaultCredentials; using (Stream stream = request.GetRequestStream())
{
//传递参数模板 Content-Disposition:form-data;name=\"{0}\"\r\n\r\n{1}
//1.1 key/value
string formdataTemplate = "Content-Disposition:form-data;name=\"{0}\"\r\n\r\n{1}";
//传递参数
if (data != null)
{
foreach (string key in data.Keys)
{
//传递参数开始标识
stream.Write(boundarybytes, , boundarybytes.Length); string formitem = string.Format(formdataTemplate, key, data[key]); byte[] formitembytes = encoding.GetBytes(formitem); stream.Write(formitembytes, , formitembytes.Length);
}
} //上传文件模板
//1.2 file
string headerTemplate = "Content-Disposition:form-data;name=\"{0}\";filename=\"{1}\"\r\nContent-Type:application/octet-stream\r\n\r\n"; byte[] buffer = new byte[**]; for (int i = ; i < files.Length; i++)
{
//上传文件标识
stream.Write(boundarybytes, , boundarybytes.Length); string header = string.Format(headerTemplate, "file" + i, Path.GetFileName(files[i])); byte[] headerbytes = encoding.GetBytes(header); stream.Write(headerbytes, , headerbytes.Length); //将文件流读入到请求流中
using (FileStream fileStream = new FileStream(files[i], FileMode.Open, FileAccess.Read))
{
int r = fileStream.Read(buffer,,buffer.Length);
stream.Write(buffer, , r);
}
} //form 结束标志
//1.3 form end
stream.Write(endbytes, , endbytes.Length);
}
//2.WebResponse
HttpWebResponse response = (HttpWebResponse)request.GetResponse();
using (StreamReader stream = new StreamReader(response.GetResponseStream()))
{ string result = stream.ReadToEnd();
return result;
}
} #endregion

2、测试:

using System;
using System.Collections;
using System.Configuration;
using System.Data;
using System.Linq;
using System.Web;
using System.Web.Security;
using System.Web.UI;
using System.Web.UI.HtmlControls;
using System.Web.UI.WebControls;
using System.Web.UI.WebControls.WebParts;
using System.Xml.Linq;
using System.Collections.Generic;
using System.Text; public partial class httpUpload_Index : System.Web.UI.Page
{
protected void Page_Load(object sender, EventArgs e)
{ }
protected void Button1_Click(object sender, EventArgs e)
{
string url = "http://172.16.1.110:8888/httpUpload/Index.aspx";string pFilename1 = FileUpload1.PostedFile.FileName; string pFilename2 = FileUpload2.PostedFile.FileName; string pFilename3 = FileUpload3.PostedFile.FileName; string[] files = new string[] { pFilename1, pFilename2, pFilename3 }; //string[] files = new string[1] { pFilename1}; Dictionary<string, string> data = new Dictionary<string, string>(); data.Add("loginName", "测试");
data.Add("password", ""); string result = HttpUploadDemo.HttpUploadFile(url, files,data,Encoding.UTF8);
Response.Write(result);
}
}

3、HTML页面

<%@ Page Language="C#" AutoEventWireup="true" CodeFile="Index.aspx.cs" Inherits="httpUpload_Index" %>

<!DOCTYPE html PUBLIC "-//W3C//DTD XHTML 1.0 Transitional//EN" "http://www.w3.org/TR/xhtml1/DTD/xhtml1-transitional.dtd">

<html xmlns="http://www.w3.org/1999/xhtml">
<head runat="server">
<title>无标题页</title>
</head>
<body>
<form id="form1" runat="server">
<div>
<asp:FileUpload ID="FileUpload1" runat="server" /> <br /><br /> <asp:FileUpload ID="FileUpload2" runat="server" /> <br /><br /> <asp:FileUpload ID="FileUpload3" runat="server" /> <br /><br /> <asp:Button ID="Button1" runat="server"
Text="上传文件" onclick="Button1_Click" />
</div>
</form>
</body>
</html>

4、服务器端

using System;
using System.Collections;
using System.Configuration;
using System.Data;
using System.Linq;
using System.Web;
using System.Web.Security;
using System.Web.UI;
using System.Web.UI.HtmlControls;
using System.Web.UI.WebControls;
using System.Web.UI.WebControls.WebParts;
using System.Xml.Linq;
using System.IO; namespace WebThreadTest.httpUpload
{
public partial class Index : System.Web.UI.Page
{
protected void Page_Load(object sender, EventArgs e)
{
string password = this.Request["password"]; string loginName = this.Request["loginName"]; HttpFileCollection files = Request.Files; if (files.Count<=)
{
Response.Write("error");
return;
}
for (int i = ; i < files.Count; i++)
{
HttpPostedFile file = files.Get(i); string filename = file.FileName; string extension = Path.GetExtension(filename); string uploadFilename = DateTime.Now.ToFileTime() + extension; string uploadDic = Server.MapPath(@"~/resource/httpUpload/"); if (!Directory.Exists(uploadDic))
{
Directory.CreateDirectory(uploadDic);
} file.SaveAs(uploadDic + uploadFilename);
}
Response.Write("success"); }
}
}

C# HTTP上传多个文件及传递参数的更多相关文章

  1. SpringMVC使用MultipartFile文件上传,多文件上传,带参数上传

    一.配置SpringMVC 二.单文件与多文件上传 三.多文件上传 四.带参数上传 一.配置SpringMVC 在spring.xml中配置: <!-- springmvc文件上传需要配置的节点 ...

  2. 强大的支持多文件上传的jQuery文件上传插件Uploadify

    支持多文件上传的jQuery文件上传插件Uploadify,目前此插件有两种版本即Flash版本和HTML5版本,对于HTML5版本会比较好的支持手机浏览器,避免苹果手机Safari浏览器不支持Fla ...

  3. spring mvc文件上传(单个文件上传|多个文件上传)

    单个文件上传spring mvc 实现文件上传需要引入两个必须的jar包    1.所需jar包:                commons-fileupload-1.3.1.jar       ...

  4. SecureCRT上传和下载文件

    SecureCRT上传和下载文件(下载默认目录) SecureCR 下的文件传输协议有ASCII .Xmodem .Ymodem .Zmodem ASCII:这是最快的传输协议,但只能传送文本文件. ...

  5. thinkphp如何一次性的上传多个文件,在文件域中可以多选?

    可以做到类似于某度网盘的样式吗? 文件夹的命名, 可以用单数, 也可以用复数, 在同一个项目中, 只要统一就好了. 毕竟项目开发不同于英语写作. 建议使用缩写, 不管是不是缩写都用单数, 这样简洁,容 ...

  6. Drupal8重命名上传的中文名文件

    完整的模块代码文件在Coding.net上,想直接使用的请前往下载:https://coding.net/u/yamus/p/chinese_rename/git/tree/master 最近吧Dru ...

  7. WordPress上传含有中文文件出现乱码

    最近打算学习安装配置WordPress,当然同时也在学习PHP+MySQL,希望以后能做一些关于WordPress定制和二次开发,包括主题和插件.在成功安装WordPress3.5中文版之后,就测试了 ...

  8. 11、只允许在主目录下上传和下载文件,不允许用putty登录

    创建用户xiao,   使其只允许在用户主目录 (/var/www/html)下上传和下载文件,不允许用putty登录 (为了安全起见,不给过多的权限) 1.创建xiao用户 [root@localh ...

  9. 每天一个linux命令(26):用SecureCRT来上传和下载文件

    用SSH管理linux服务器时经常需要远程与本地之间交互文件.而直接用SecureCRT自带的上传下载功能无疑是最方便的,SecureCRT下的文件传输协议有ASCII.Xmodem.Zmodem. ...

随机推荐

  1. Python 零基础 快速入门 趣味教程 (咪博士 海龟绘图 turtle) 7. 条件循环

    条件循环能够让程序在条件成立时(即为真时)重复执行循环体中的语句.如果条件一直成立(即永远不会为假),则循环会一直进行下去,不会停止.如果初始时,条件不成立,则循环 1 次也不会执行.Python 中 ...

  2. Python 零基础 快速入门 趣味教程 (咪博士 海龟绘图 turtle) 6. 条件

    前面的教程中,我们已经可以让小海龟绘制出各种图形了.但是,所有绘图的代码都是预先编好的,程序一旦运行起来,运行结果(绘制的图形)就是固定不变的.这一节中,咪博士将教大家如何让海龟响应用户的输入. im ...

  3. selenium之调用Javascript

    selenium调用Javascript使用方法: driver.execute_script(js) 使用JS获取元素文本值,代码片段如下: ...... js = "return $(' ...

  4. BZOJ3522[Poi2014]Hotel——树形DP

    题目描述 有一个树形结构的宾馆,n个房间,n-1条无向边,每条边的长度相同,任意两个房间可以相互到达.吉丽要给他的三个妹子各开(一个)房(间).三个妹子住的房间要互不相同(否则要打起来了),为了让吉丽 ...

  5. BZOJ1127 POI2008KUP(悬线法)

    首先显然地,如果某个格子的权值超过2k,其一定不在答案之中:如果在[k,2k]中,其自身就可以作为答案.那么现在我们只需要考虑所选权值都小于k的情况. 可以发现一个结论:若存在一个权值都小于k的矩阵其 ...

  6. Jquery 行选中背景色

    直接上代码: 懒得以后网上在查了,拷贝直接可用 Style: .tbSelectCss { background-color:#d5f4fe; } Html: <table name=" ...

  7. Maven依赖中的scope详解

    scope的分类 compile 默认就是compile,什么都不配置也就是意味着compile.compile表示被依赖项目需要参与当前项目的编译,当然后续的测试,运行周期也参与其中,是一个比较强的 ...

  8. Ant Trip HDU - 3018(欧拉路的个数 + 并查集)

    题意: Ant Tony和他的朋友们想游览蚂蚁国各地. 给你蚂蚁国的N个点和M条边,现在问你至少要几笔才能所有边都画一遍.(一笔画的时候笔不离开纸) 保证这M条边都不同且不会存在同一点的自环边. 也就 ...

  9. MT【7】伯努利不等式

    评:伯努利不等式: 若$r\le0$或者$r\ge1$,$(1+x)^r\ge1+rx$, 若$0\le r\le1$,$(1+x)^r\le1+rx$

  10. 【总结】 Lucas定理

    \(Lucas\)定理: \(C^x_y≡C^{x/p}_{y/p}*C^{x\%p}_{y\%p} ~~(mod~p)\) 证明不会2333 void pre(){ A[0]=A[1]=B[0]=B ...