做一个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;
}
}

成功实现了从手机端上传图片功能,在此基础上可以继续添加别的功能,在不同应用中用到。

最新文章

  1. 如何处理CSS3属性前缀
  2. C可变参数的函数
  3. Dw CS 破解
  4. ubtuntu 下安装Erlang R17
  5. 4.总结近5周以来的github上的工作情况,以图表方式分析你小组的工作情况、存在的问题及解决的方案。(尤心心)
  6. Rs2008内存管理策略
  7. html随笔
  8. ectouch第十讲 之ecshop中 dwt, lbi 文件详解
  9. UVa 11300 Spreading the Wealth 分金币
  10. JQuery(一) 入门
  11. ECharts中文显示为Unicode码
  12. Java Me-List控件的用法案例
  13. python模块导入的方法与区别
  14. UVA 673 Parentheses Balance (栈)
  15. 【非原创】C++类成员函数的重载、覆盖和隐藏
  16. java项目使用mvn打包时,出现数据库连接错误
  17. python 全栈开发,Day85(Git补充,随机生成图片验证码)
  18. Go语言之高级篇beego框架之config、httplib、context
  19. Windows XP Professional产品序列号
  20. VS2015 无法启动 IIS服务器

热门文章

  1. BZOJ2212——线段树合并
  2. eas之辅助编辑功能
  3. 【剑指Offer】36、两个链表的第一个公共结点
  4. encodeURI和encodeURIComponent的区别?
  5. Win32_Window(day02)
  6. Django admin(四)一些有用定制
  7. ZOJ 3362 Beer Problem
  8. MySQL Migration Toolkit v2.1特别版
  9. 0923如何利用mysqlbinlog日志闪回
  10. ASP.NET--IIS的Http请求流程