View Code
namespace SilverlightDownAndUploadFile
{
public partial class MainPage : UserControl
{
private OpenFileDialog ofd;
private AutoResetEvent autoEvent = new AutoResetEvent(false);
public MainPage()
{
InitializeComponent();
}
private static HttpWebRequest CreateWebRequest(string requestUriString, string httpMethod)
{
try
{
string ticks = Guid.NewGuid().ToString();
requestUriString = requestUriString + "/" + ticks;
HttpWebRequest request = (HttpWebRequest)WebRequestCreator.ClientHttp.Create(new Uri(requestUriString));
request.Method = httpMethod;
return request;
}
catch (Exception ex)
{
return null;
}
}
#region 上传
private void ReadFileToRequestStream(BinaryReader fileStream, HttpWebRequest request)
{
try
{
request.BeginGetRequestStream((requestAsyncResult) =>
{
Stream requestStream = null;
try
{
requestStream = request.EndGetRequestStream(requestAsyncResult);
if (null != requestStream)
{
SilverlightMultipartFormData form = new SilverlightMultipartFormData();
//form.AddFormField("devilField", "中国人");
int maxBufferLen = 25 * 1024;
byte[] buffer = new byte[maxBufferLen];
int bytesRead = fileStream.Read(buffer, 0, maxBufferLen);
int i = 0;
while (bytesRead != 0)
{
i++;
byte[] newBuffer = GetNewBuffer(buffer, bytesRead);
form.AddStreamFile("package" + i, "package" + i, newBuffer);
buffer = new byte[maxBufferLen];
bytesRead = fileStream.Read(buffer, 0, maxBufferLen);
}
form.PrepareFormData();//结束
fileStream.Close();
request.ContentType = "multipart/form-data; boundary=" + form.Boundary;
foreach (byte byt in form.GetFormData())
{
requestStream.WriteByte(byt);
}
requestStream.Close();
autoEvent.Set();//开始上传
}
}
catch (Exception ex)
{ }
finally
{
if (null != requestStream)
requestStream.Close();
if (null != fileStream)
fileStream.Close();
}
}, null);
}
catch (Exception ex)
{
}
}
private void RequestAndUploadFile(BinaryReader br)
{
try
{
if (null == br) return;
HttpWebRequest request = CreateWebRequest("http://localhost:13953/Upload.ashx", "POST");
ReadFileToRequestStream(br, request);
autoEvent.WaitOne();//等待流写入完毕
//上传
request.BeginGetResponse(
(asyncResult) =>
{
if (request.HaveResponse)
{
try
{
WebResponse response = request.EndGetResponse(asyncResult);
Stream responseStream = response.GetResponseStream();
int bufferLen = 1024;
byte[] buffer = new byte[bufferLen];
int bytesRead = responseStream.Read(buffer, 0, bufferLen);
responseStream.Close();
string responseText = System.Text.Encoding.UTF8.GetString(buffer, 0, bytesRead);
ShowUploadResult(true);
}
catch (Exception ex)
{
ShowUploadResult(false);
}
}
}, null);
}
catch (Exception ex)
{
ShowUploadResult(false);
}
}
private void btnUpload_Click(object sender, RoutedEventArgs e)
{
ofd = new OpenFileDialog();
ofd.Filter = "(*.rar)|*.rar";
ofd.Multiselect = false;
if (ofd.ShowDialog() == true)
{
uploadProgress.Visibility = Visibility.Visible;
BinaryReader br = new BinaryReader(ofd.File.OpenRead());
RequestAndUploadFile(br);
}
}
private void ShowUploadResult(bool isSucess)
{
this.Dispatcher.BeginInvoke(
() =>
{
uploadProgress.Visibility = Visibility.Collapsed;
if (isSucess)
MessageBox.Show("上传成功");
else
MessageBox.Show("上传失败.");
});
}
#endregion
///
/// 获了实际接读取到的字节数组
///
///
///
///
private byte[] GetNewBuffer(byte[] buffer, int bytesRead)
{
byte[] newBuffer = new byte[bytesRead];
for (int i = 0; i < bytesRead; i++)
{
newBuffer[i] = buffer[i];
}
return newBuffer;
}
}
}
|