[代码分享]上传文件到服务器(普通表单数据和文件)
这是通过嗅探工具 得到表单提单上传的数据。。
POST xxxxxxx HTTP/1.1
Accept: image/jpeg, application/x-ms-application, image/gif, application/xaml+xml, image/pjpeg, application/x-ms-xbap, application/vnd.ms-excel, application/vnd.ms-powerpoint, application/msword, */*
Referer: xxxxxxx
Accept-Language: zh-CN
User-Agent: Mozilla/4.0 (compatible; MSIE 8.0; Windows NT 6.1; Trident/4.0; SLCC2; .NET CLR 2.0.50727; .NET CLR 3.5.30729; .NET CLR 3.0.30729; Media Center PC 6.0; .NET4.0C; .NET4.0E; InfoPath.2)
Content-Type: multipart/form-data; boundary=---------------------------7dbdd139a1514
Accept-Encoding: gzip, deflate
Host: xxxxxxx
Content-Length: xxxxxxx
Connection: Keep-Alive
Pragma: no-cache
Cookie: xxxxxxx
-----------------------------7dbdd139a1514
Content-Disposition: form-data; name="Employee.EmployeeId"
1
-----------------------------7dbdd139a1514
Content-Disposition: form-data; name="filePhoto"; filename="P1100353.JPG"
Content-Type: image/pjpeg
XXXXXXXXXXXXXXXXXXXXXXXXXXXXXXXX
其中,Content-Type的内容为: multipart/form-data;我们在做上传文件时,都要设置表单的 enctype="multipart/form-data"的,设置这个属性后,上传的数据就会变成
-----------------------------7dbdd139a1514
Content-Disposition: form-data; name="Employee.EmployeeId"
1
这样的。
下面是代码.
[code=C]
using System;
using System.Collections.Generic;
using System.Linq;
using System.Text;
using System.Net;
using System.Collections;
using System.Net.Mime;
using System.Globalization;
using System.IO;
namespace Helper
{
/// <summary>
/// 上传
/// </summary>
public class UploadHelper
{
// 边界数据
/// <summary>
/// 边界数据(boundary = "---------------------------7db38610205dc")
/// </summary>
public string BoundaryData { get; set; }
//数据编码
/// <summary>
/// 数据编码,默认为UTF8
/// </summary>
public Encoding Encoding { get; set; }
//尾部数据
/// <summary>
/// 尾部数据
/// </summary>
private string EndData
{
get
{
return Environment.NewLine + "--" + this.BoundaryData + "--" + Environment.NewLine;
}
}
/// <summary>
/// ctor
/// </summary>
public UploadHelper()
{
string boundary = "---------------------------" + DateTime.Now.Ticks.ToString("x", NumberFormatInfo.CurrentInfo);
this.BoundaryData = boundary;
this.Encoding = Encoding.UTF8;
}
/// <summary>
/// ctor
/// </summary>
public UploadHelper(Encoding encoding)
{
this.Encoding = encoding;
}
/// <summary>
/// ctor
/// </summary>
public UploadHelper(string boundary, Encoding encoding)
{
this.BoundaryData = boundary;
this.Encoding = encoding;
}
//普通 表单属性 数据
/// <summary>
/// 普通 表单属性 数据(-----------------------------7db210302011cc\r\nContent-Disposition: form-data; name="Mobile")
/// </summary>
/// <param name="fieldName">属性名 </param>
/// <param name="fieldValue">属性值 </param>
/// <returns> </returns>
public byte[] CreateNormalFieldData(string fieldName, string fieldValue)
{
string data = "{1}{0}Content-Disposition: form-data; name=\"{2}\"{0}{0}{3}{0}";
data = string.Format(data, Environment.NewLine, "--" + this.BoundaryData, fieldName, fieldValue);
return this.Encoding.GetBytes(data);
}
//上传文件属性 数据
/// <summary>
/// 上传文件属性 数据
/// </summary>
/// <param name="fieldName">属性名 </param>
/// <param name="fileName">上传文件名 </param>
/// <param name="content_type">上传文件类型 </param>
/// <param name="fileBytes">byte数组 </param>
/// <returns> </returns>
public byte[] CreateFileFieldData(string fieldName, string fileName, string content_type, byte[] fileBytes)
{
string data = "{1}{0}Content-Disposition: form-data; name=\"{2}\"; filename=\"{3}\"{0}Content-Type: {4}{0}{0}";
data = string.Format(data, Environment.NewLine, "--" + this.BoundaryData, fieldName, fileName, content_type);
byte[] dataBytes = this.Encoding.GetBytes(data);
byte[] resultBytes = new byte[dataBytes.Length + fileBytes.Length];
dataBytes.CopyTo(resultBytes, 0);
fileBytes.CopyTo(resultBytes, dataBytes.Length);
return resultBytes;
}
//上传文件属性 数据
/// <summary>
/// 上传文件属性 数据
/// </summary>
/// <param name="fieldName">属性名 </param>
/// <param name="fileName">上传文件名 </param>
/// <param name="content_type">上传文件类型 </param>
/// <param name="stream">上传的文件流 </param>
/// <returns> </returns>
public byte[] CreateFileFieldData(string fieldName, string fileName, string content_type, Stream stream)
{
return CreateFileFieldData(fieldName, fileName, content_type, UploadHelper.StreamToBytes(stream));
}
//上传文件
/// <summary>
/// 上传 文件
/// </summary>
/// <param name="webClient">WebClient </param>
/// <param name="uploadUrl">上传地址 </param>
/// <param name="length">上传数据的长度 </param>
/// <param name="postDataBytes">上传的数据 </param>
/// <returns>服务响应的结果 </returns>
public string UploadData(WebClient webClient, string uploadUrl, int length, params byte[][] postDataBytes)
{
webClient.Headers.Add("Content-Type", "multipart/form-data; boundary=" + this.BoundaryData);
byte[] ends = this.Encoding.GetBytes(this.EndData);
byte[] bytes = new byte[length + ends.Length];
int eachLength = 0;
foreach (var item in postDataBytes)
{
item.CopyTo(bytes, eachLength);
eachLength += item.Length;
}
ends.CopyTo(bytes, eachLength);
byte[] responseData = webClient.UploadData(uploadUrl, bytes);
string resultStr = this.Encoding.GetString(responseData);
resultStr = resultStr.Replace("\t", "").Replace("\r", "").Replace("\n", "");
return resultStr;
}
// 上传 普通数据
/// <summary>
/// 上传 普通数据
/// </summary>
/// <param name="webClient">WebClient </param>
/// <param name="uploadUrl">上传地址 </param>
/// <param name="postData">数据 </param>
/// <returns> </returns>
public string UploadData(WebClient webClient, string uploadUrl, string postData)
{
webClient.Headers.Add("Content-Type", "application/x-www-form-urlencoded");
byte[] bytes = this.Encoding.GetBytes(postData);
byte[] responseData = webClient.UploadData(uploadUrl, "POST", bytes);
string srcString = this.Encoding.GetString(responseData);
srcString = srcString.Replace("\t", "").Replace("\r", "").Replace("\n", "");
return srcString;
}
//将 Stream 转成 byte[]
/// <summary>
/// 将 Stream 转成 byte[]
/// </summary>
/// <param name="stream">流 </param>
/// <returns>byte[] </returns>
public static byte[] StreamToBytes(Stream stream)
{
byte[] bytes = new byte[stream.Length];
stream.Read(bytes, 0, bytes.Length);
stream.Seek(0, SeekOrigin.Begin);
return bytes;
}
}
}
[/code]
上传时,需要设置在Content-Type属性,还有boundary(边界的意思).
在数据的尾部,还要加上
[code=C]Environment.NewLine + "--" + this.BoundaryData + "--" + Environment.NewLine[/code],表示此次数据的结束。这样,就完成了一次上传.
[解决办法]
3Q
[解决办法]