void btSave_Click(object sender, RoutedEventArgs e)
{
OpenFileDialog op = new OpenFileDialog();
op.Multiselect = false;
if(_pvFlag==0)
op.Filter = "JPG 图片 (*.jpg)|*.jpg|PNG 图片 (*.png)|*.png";
else
op.Filter = "Wav 文件(*.wav)|*.wav";
if (op.ShowDialog() == true)
{
string filename = op.File.Name;
string extension = filename.Substring(filename.LastIndexOf('.'));
uploadName = DateTime.Now.ToString("yyyyMMddhhmmss") + extension;
this.tbPath.Text = uploadName;
FileStream fs = op.File.OpenRead();
UploadUserPhoto(uploadName,fs);
// this.imageShow.Source = new BitmapImage(new Uri("/" + strFoldName + "/" + uploadName, UriKind.Relative));
}
}
private void UploadUserPhoto(string fileName, Stream data)
{
string _strSaveFloder = "UploadFile";
Uri uri = new Uri(string.Format("/WebClientUpload.ashx?fileName={0}&fileFold={1}", fileName, _strSaveFloder), UriKind.Relative);
WebClient client = new WebClient();
client.OpenWriteCompleted += (sender, e) =>
{
PushData(data, e.Result);
e.Result.Close();
data.Close();
};
client.OpenWriteAsync(uri);
}
private void PushData(Stream input, Stream output)
{
byte[] buffer = new byte[4096];
int bytesRead;
while ((bytesRead = input.Read(buffer, 0, buffer.Length)) != 0)
{
output.Write(buffer, 0, bytesRead);
}
}
using System;
using System.Collections.Generic;
using System.Linq;
using System.Web;
using System.IO;
namespace test.Web
{
/// <summary>
/// Summary description for WebClientUpload
/// </summary>
public class WebClientUpload : IHttpHandler
{
public void ProcessRequest(HttpContext context)
{
string fileName = context.Request.QueryString["fileName"].ToString();
string fileFold = context.Request.QueryString["fileFold"].ToString();
Stream s = context.Request.InputStream;
FileStream fs = new FileStream(context.Server.MapPath(fileFold) + "/" + fileName, FileMode.Create);
byte[] bytes = new byte[s.Length];
s.Read(bytes, 0, bytes.Length);
fs.Write(bytes, 0, bytes.Length);
fs.Flush();
fs.Close();
}
public bool IsReusable
{
get
{
return false;
}
}
}
}