C#版的MsMultiPartFormData(适用于Silverlight平台上传二进流数据到服务器)
在这之前,我已经写过了AS3版本的MsMultiPartFormData,给Flash上传Http的Form用,写过C++版的MsMultiPartFormData,给C++的朋友上传Http的Form用。在这里,再写一个C#版本的,呵呵,主要是想给Silverlight用的。具体代码如何下(至于Http的multipart/form-data格式,大家还是得去找资料来看看了):
************************************************************
#region summary
/*----------------------------------------------------------------------------
*
* 功能描述: 核心功能是什么
*
* 作 者: qwliang
*
* 修改日期: 2010/10/10
*
* 职 责 : 做些什么事情
*
* 特 性 : 有什么特点
*----------------------------------------------------------------------------*/
#endregion
using System;
using System.Collections;
using System.Text;
using System.Collections.Generic;
namespace MyRBS
{
public class MsMultiPartFormData
{
private List<byte> formData;
public String Boundary = "---------------------------7db1851cd1158";
private String fieldName = "Content-Disposition: form-data; name=\"{0}\"";
private String fileContentType = "Content-Type: {0}";
private String fileField = "Content-Disposition: form-data; name=\"{0}\"; filename=\"{1}\"";
private Encoding encode = Encoding.GetEncoding("UTF-8");
public MsMultiPartFormData()
{
formData = new List<byte>();
}
public void AddFormField(String FieldName, String FieldValue)
{
String newFieldName = fieldName;
newFieldName = string.Format(newFieldName, FieldName);
formData.AddRange(encode.GetBytes("--" + Boundary + "\r\n"));
formData.AddRange(encode.GetBytes(newFieldName + "\r\n\r\n"));
formData.AddRange(encode.GetBytes(FieldValue + "\r\n"));
}
public void AddFile(String FieldName, String FileName, byte[] FileContent, String ContentType)
{
String newFileField = fileField;
String newFileContentType = fileContentType;
newFileField = string.Format(newFileField, FieldName, FileName);
newFileContentType = string.Format(newFileContentType, ContentType);
formData.AddRange(encode.GetBytes("--" + Boundary + "\r\n"));
formData.AddRange(encode.GetBytes(newFileField + "\r\n"));
formData.AddRange(encode.GetBytes(newFileContentType + "\r\n\r\n"));
formData.AddRange(FileContent);
formData.AddRange(encode.GetBytes("\r\n"));
}
public void AddStreamFile(String FieldName, String FileName, byte[] FileContent)
{
AddFile(FieldName, FileName, FileContent, "application/octet-stream");
}
public void PrepareFormData()
{
formData.AddRange(encode.GetBytes("--" + Boundary + "--"));
}
public List<byte> GetFormData()
{
return formData;
}
}
}
************************************************************
具体的调用例子如下:
***********************************************************
WebRequest request = WebRequest.Create("http://localhost/Test.aspx");
MsMultiPartFormData form = new MsMultiPartFormData();
form.AddFormField("devilField", "中国人");
FileStream file = new FileStream("F:\\\\Devil_Files\\My_File\\devil.jpg", FileMode.Open);
byte[] bb = new byte[file.Length];
file.Read(bb, 0, (int)file.Length);
file.Close();
form.AddStreamFile("fileUpload", "中国人图片.jpg", bb);
form.PrepareFormData();
request.ContentType = "multipart/form-data; boundary=" + form.Boundary;
request.Method = "POST";
Stream stream = request.GetRequestStream();
foreach (var b in form.GetFormData())
{
stream.WriteByte(b);
}
stream.Close();
WebResponse response = request.GetResponse();
************************************************************