7,657
社区成员
发帖
与我相关
我的任务
分享
void pct_Completed(object sender, PhotoResult e)
{
if (e.Error != null)
{
MessageBox.Show(e.Error.Message);
}
else if (e.TaskResult.Equals(TaskResult.OK))
{
UploadStateObject uso = new UploadStateObject();
uso.Stream = e.ChosenPhoto;
uso.Boundary = "----WebKitFormBoundary3jJRGWjdL0V3CTvW";
uso.OriginalFileName = e.OriginalFileName;
HttpWebRequest request = HttpWebRequest.CreateHttp(new Uri(String.Format(liveUpload, id), UriKind.Absolute));
request.Method = "POST";
request.CookieContainer = cc;
request.ContentType = "multipart/form-data; boundary=" + uso.Boundary;
request.AllowAutoRedirect = true;
uso.Request = request;
request.BeginGetRequestStream(GetRequestStream, uso);
pi.IsVisible = true;
}
}
private void GetRequestStream(IAsyncResult ar)
{
UploadStateObject uso = ar.AsyncState as UploadStateObject;
Stream bw = uso.Request.EndGetRequestStream(ar);
String[] fileName = uso.OriginalFileName.Split(new char[] { '\\' });
Byte[] boundary = Encoding.UTF8.GetBytes(uso.Boundary + "\r\n");
Byte[] disposition = Encoding.UTF8.GetBytes("Content-Disposition: form-data; name=\"file\"; filename=\"wp.jpg\"\r\n");
Byte[] contentType = Encoding.UTF8.GetBytes("Content-Type: image/jpeg\r\n\r\n");
Byte[] endBoundary = Encoding.UTF8.GetBytes("\r\n" + uso.Boundary + "--");
Byte[] ff = Encoding.UTF8.GetBytes("Content-Disposition: form-data; name=\"localUrl\"");
Byte[] va = Encoding.UTF8.GetBytes("\r\n\r\nwp.jpg\r\n");
bw.Write(boundary, 0, boundary.Length);
bw.Write(ff, 0, ff.Length);
bw.Write(va, 0, va.Length);
bw.Write(boundary, 0, boundary.Length);
bw.Write(disposition, 0, disposition.Length);
bw.Write(contentType, 0, contentType.Length);
Byte[] buffer = new Byte[2048];
Int32 len = 0;
while ((len = uso.Stream.Read(buffer, 0, buffer.Length)) != 0)
{
bw.Write(buffer, 0, len);
}
bw.Write(endBoundary, 0, endBoundary.Length);
bw.Close();
uso.Stream.Close();
this.Dispatcher.BeginInvoke(() =>
{
MessageBox.Show("发送完成");
});
uso.Request.BeginGetResponse(GetResponse, uso);
}
private void GetResponse(IAsyncResult ar)
{
UploadStateObject uso = ar.AsyncState as UploadStateObject;
HttpWebResponse response = (HttpWebResponse)uso.Request.EndGetResponse(ar);
}
class UploadStateObject
{
public HttpWebRequest Request { get; set; }
public Stream Stream { get; set; }
public String Boundary { get; set; }
public String OriginalFileName { get; set; }
}