wp7图片上传服务器
2024-08-31 07:58:53
做一个wp7手机上传图片到服务器的功能,具体丝路是在手机端做一个照相或者选择图片的功能,点击上传,在服务器端做一个一般处理程序,接受上传的文件,存入文件夹,下面是主要代码:
手机端代码:
/// <summary>
/// 点击上传照片
/// </summary>
/// <param name="sender"></param>
/// <param name="e"></param>
private void btn_Upload_Click(object sender, RoutedEventArgs e)
{
PhotoChooserTask task = new PhotoChooserTask();
task.PixelHeight = ;
task.PixelWidth = ;
task.ShowCamera = true;
task.Completed += new EventHandler<PhotoResult>(task_Completed);
task.Show(); }
//选择图片完成事件,上传图片操作
void task_Completed(object sender, PhotoResult e)
{
const int BLOCK_SIZE = ; //Code参数可以是存入服务器图片的命名
Uri uri = new Uri("http://localhost:58556/WebClientUpLoadHandler.ashx?Code=123456", UriKind.Absolute);
WebClient wc = new WebClient();
wc.AllowReadStreamBuffering = true;
wc.AllowWriteStreamBuffering = true; wc.OpenWriteCompleted += (s, args) =>
{
using (BinaryReader br = new BinaryReader(e.ChosenPhoto))
{
using (BinaryWriter bw = new BinaryWriter(args.Result))
{
long bCount = ;
long fileSize = e.ChosenPhoto.Length;
byte[] bytes = new byte[BLOCK_SIZE];
do
{
bytes = br.ReadBytes(BLOCK_SIZE);
bCount += bytes.Length;
bw.Write(bytes);
} while (bCount < fileSize);
}
}
}; wc.WriteStreamClosed += (s, args) =>
{
MessageBox.Show("上传成功!");
}; wc.OpenWriteAsync(uri, "POST");
}
在服务器端建一个一般处理程序WebClientUpLoadHandler.ashx
代码为:
string name = context.Request.Params["Code"].ToString();
//获取从Silverlight客户端传来的信息
int length = context.Request.ContentLength;
byte[] bytes = context.Request.BinaryRead(length);
string uploadFolder = System.AppDomain.CurrentDomain.BaseDirectory + "\\upload"; //目录不存在则新建
if (!Directory.Exists(uploadFolder))
{
Directory.CreateDirectory(uploadFolder);
} ////写入文件
try
{
using (FileStream fs = new FileStream(uploadFolder + "\\" + name+".jpg", FileMode.Create, FileAccess.Write))
{
fs.Write(bytes, , bytes.Length);
}
context.Response.Write("服务器端成功");
}
catch { context.Response.Write("写入失败"); }
} public bool IsReusable
{
get
{
return false;
}
}
成功实现了从手机端上传图片功能,在此基础上可以继续添加别的功能,在不同应用中用到。
最新文章
- 如何处理CSS3属性前缀
- C可变参数的函数
- Dw CS 破解
- ubtuntu 下安装Erlang R17
- 4.总结近5周以来的github上的工作情况,以图表方式分析你小组的工作情况、存在的问题及解决的方案。(尤心心)
- Rs2008内存管理策略
- html随笔
- ectouch第十讲 之ecshop中 dwt, lbi 文件详解
- UVa 11300 Spreading the Wealth 分金币
- JQuery(一) 入门
- ECharts中文显示为Unicode码
- Java Me-List控件的用法案例
- python模块导入的方法与区别
- UVA 673 Parentheses Balance (栈)
- 【非原创】C++类成员函数的重载、覆盖和隐藏
- java项目使用mvn打包时,出现数据库连接错误
- python 全栈开发,Day85(Git补充,随机生成图片验证码)
- Go语言之高级篇beego框架之config、httplib、context
- Windows XP Professional产品序列号
- VS2015 无法启动 IIS服务器