这种上传控件的方法,有时候单纯的靠拼接是不行的,楼主可以尝试一下这种方案测试一下,Htttphelper暂时不支持分流上传
[C#] 纯文本查看 复制代码 using System;
using System.Collections.Generic;
using System.IO;
using System.Linq;
using System.Text;
using System.Threading.Tasks;
namespace Msf.Engine.Network.NetworkTool
{
public sealed class MultipartFormData : IDisposable
{
public const string Boundary = "--the_great_maoshu--";
public static string ContentType
{
get { return "multipart/form-data; boundary=" + Boundary; }
}
private MemoryStream _stream;
public MemoryStream Stream
{
get { return _stream ?? (_stream = new MemoryStream()); }
}
public MultipartFormData()
{
}
public void AddContent(string name, string value)
{
var sp = string.Format("--{0}\r\n", Boundary);
sp += string.Format(
"Content-Disposition: form-data; name=\"{0}\"; \r\n\r\n{1}",
name,
value);
var data = Encoding.UTF8.GetBytes(sp);
Stream.Write(data, 0, data.Length);
}
public void AddContent(string name, string fileName, byte[] fileData)
{
var sp = string.Format("--{0}\r\n", Boundary);
sp += string.Format(
"Content-Disposition: form-data; name=\"{0}\"; filename=\"{1}\"\r\nContent-Type: application/octet-stream\r\n\r\n",
name,
fileName);
var data = Encoding.UTF8.GetBytes(sp);
Stream.Write(data, 0, data.Length);
Stream.Write(fileData, 0, fileData.Length);
data = Encoding.UTF8.GetBytes("\r\n");
Stream.Write(data, 0, data.Length);
}
public byte[] GetPostData()
{
var sp = string.Format("--{0}--\r\n", Boundary);
var data = Encoding.UTF8.GetBytes(sp);
Stream.Write(data, 0, data.Length);
Stream.Position = 0;
return Stream.ToArray();
}
public void Dispose()
{
if (_stream != null) _stream.Dispose();
_stream = null;
}
}
}
http://www.cnblogs.com/yydcdut/p/3736667.html
|