1. webService
  2.  
  3. /// <summary>
  4. /// 上传图片webServer 的摘要说明
  5. /// </summary>
  6. [WebService(Namespace = "http://tempuri.org/")]
  7. [WebServiceBinding(ConformsTo = WsiProfiles.BasicProfile1_1)]
  8. [ToolboxItem(false)]
  9. public class WebService1 : System.Web.Services.WebService
  10. {
  11. [WebMethod]
  12. public bool UpdateFile(byte[] content, string pathand,string filename)
  13. {
  14. string pathandname = pathand + filename;
  15. int index = pathandname.LastIndexOf(".");
  16. if (index == )
  17. {
  18. return false;
  19. }
  20. else
  21. {
  22. string extended = string.Empty;
  23. if (index + == pathandname.Length)
  24. {
  25. return false;
  26. }
  27. else
  28. {
  29. extended = pathandname.Substring(index + );
  30. if (extended == "jpeg" || extended == "gif" || extended == "jpg" ||
    extended == "bmp" || extended == "png")
  31. {
  32. try
  33. {
  34. if (!Directory.Exists(@pathand))//若文件夹不存在则新建文件夹
  35. {
  36. Directory.CreateDirectory(@pathand); //新建文件夹
  37. }
  38.  
  39. //File.WriteAllBytes(Server.MapPath(pathandname), content);
  40. File.WriteAllBytes(pathandname, content);
  41. return true;
  42. }
  43. catch (Exception ex)
  44. {
  45. return false;
  46. }
  47. }
  48. else
  49. {
  50. return false;
  51. }
  52. }
  53. }
  54. }
  55. }
  56.  
  57. //测试
  58.  
  59. private void btnSaveServer_Click(object sender, EventArgs e)
  60. {
  61. OpenFileDialog fileDialog = new OpenFileDialog();
  62. if (fileDialog.ShowDialog() == DialogResult.OK)
  63. {
  64. string pathand = CommonClass.Config.GetAppSettings<string>("ProductImageUrl",
    @"D:\FSTERP\ProductImage\");
  65. string imagename = "mylove";
  66. bool uploadResult = UploadImageWebService(fileDialog.FileName, pathand, imagename);
  67. if (uploadResult)
  68. MessageBox.Show("上传成功!");
  69. else
  70. MessageBox.Show("上传失败!");
  71. }
  72. }
  73. /// <summary>
  74. /// 上传图片[通过webServer]
  75. /// </summary>
  76. /// <param name="filename">选择图片路径[默认选择文件包括后缀名]</param>
  77. /// <param name="pathand">上传服务器文件夹[文件夹不存在则新建]</param>
  78. /// <param name="imagename">上传后图片文件名[不包括后缀名]</param>
  79. /// <returns>上传结果</returns>
  80. public bool UploadImageWebService(string filename, string pathand, string imgname)
  81. {
  82.  
  83. string extension = Path.GetExtension(filename).ToLower().Replace(".", "");
  84. string paramSuffix = "|" + CommonClass.Config.GetAppSettings<string>("ImageFormat",
    "jpg|jpge|gif|bmp|png") + "|";
  85. int pi = paramSuffix.IndexOf("|" + extension + "|");
  86. if (pi < )
  87. {
  88. MessageBox.Show("仅能上传jpg|jpge|gif|bmp|png格式的图片!");
  89. return false;
  90. }
  91. else
  92. {
  93. FileInfo fileInfo = new FileInfo(filename);
  94. if (fileInfo.Length > )
  95. {
  96. MessageBox.Show("上传的图片不能大于20K");
  97. }
  98. else
  99. {
  100. //Stream file = fileDialog.OpenFile();
  101. FileStream file = new FileStream(filename, FileMode.Open, FileAccess.Read);
  102. byte[] bytes = new byte[file.Length];
  103. file.Read(bytes, , bytes.Length);
  104. //实例化WebService服务。ServiceReference1是我们在添加引用时设置的命名空间
  105. WebService.WebService1 webservice = new FSTERP.WebService.WebService1();
  106. DateTime time = DateTime.Now;
  107. //重命名图片的名称与路径
  108. //string pathand = CommonClass.Config.GetAppSettings<string>("ProductImageUrl",
    @"D:\FSTERP\ProductImage\");
  109. string imagename = imgname + "." + extension;
  110. //string pathandname = pathand + imagename;
  111. if (webservice.UpdateFile(bytes, pathand, imagename))
  112. {
  113. return true;
  114. }
  115. else
  116. {
  117. return false;
  118. }
  119. }
  120. }
  121. return false;
  122. }

测试图片

版权声明:本文为博主原创文章,未经博主允许不得转载。

webService上传图片的更多相关文章

  1. Sql server之路 (六)上传服务器图片

    原理: 上传图片的名字 插入到数据库里 上传图片的内容(二进制数据) 写到服务器指定的目录下 下次读取图片的时候 从数据库里的指定字段里读取图片文件名 从数据库的指定路径下 拼串成完成的路径 就可以下 ...

  2. webservice跨域上传图片

    1.上传文件,在一般处理程序中处理 //1.接收post过来的文件 HttpPostedFile file = context.Request.Files[]; || file.ContentLeng ...

  3. android之调用webservice 实现图片上传

    转:http://www.cnblogs.com/top5/archive/2012/02/16/2354517.html public void testUpload(){ try{ String ...

  4. WebService上传下载图片

    WebService服务端 接受要上传的图片 public string UploadImg(byte[] fileBytes, int id) { try { string filePath = M ...

  5. Android使用ksoap2调用C#中的webservice实现图像上传

    目录: 一. android使用ksoap2调用webservice 二. 异步调用 三. Android使用ksoap2调用C#中的webservice实现图像上传参考方法 四. 图像传输中Base ...

  6. iOS开发之结合asp.net webservice实现文件上传下载

    iOS开发中会经常用到文件上传下载的功能,这篇文件将介绍一下使用asp.net webservice实现文件上传下载. 首先,让我们看下文件下载. 这里我们下载cnblogs上的一个zip文件.使用N ...

  7. 博客使用BOS上传图片

    1.博客平台的选定 从大学开始做个人主页算起,最开始是使用html,CSSS写简单的页面,后面大学毕业之后接触到了WordPress,就开始用WordPress搭建网站.现在还维护着一个农村网站.ht ...

  8. nodejs利用ajax实现网页无刷新上传图片

    nodejs利用ajax实现网页无刷新上传图片 标签(空格分隔): nodejs 通常情况下上传图片是要通过提交form表单来实现的,但是这又不可避免的产生了网页转. 利用ajax技术和FormDat ...

  9. webService

    什么是webService WebService,顾名思义就是基于Web的服务.它使用Web(HTTP)方式,接收和响应外部系统的某种请求.从而实现远程调用.  1:从WebService的工作模式上 ...

随机推荐

  1. java 静态代理总结

    首先:定义一个抽象类或借口 例: package JingTai; public interface JingTai1 { public void show(); } 接着:定义一个被代理的类,他需要 ...

  2. newcoder 筱玛的迷阵探险(搜索 + 01字典树)题解

    题目描述 筱玛是个快乐的男孩子. 寒假终于到了,筱玛决定请他的朋友们一起来玩迷阵探险. 迷阵可以看做一个n×nn×n的矩阵A,每个格子上有一个有一个数Ai,j. 入口在左上角的(1,1)处,出口在右下 ...

  3. 【Spring Security】四、自定义页面

    在前面例子中,登陆页面都是用的Spring Security自己提供的,这明显不符合实际开发场景,同时也没有退出和注销按钮,因此在每次测试的时候都要通过关闭浏览器来注销达到清除session的效果. ...

  4. Asp.net 之 window 操作命令

    命令:cmd  打开执行窗口 命令:inetmgr.打开iis管理器 命令:dcomcnfg 打开组件服务 命令:regedit   打开注册表

  5. 题解——洛谷P1250 种树(差分约束)

    一道看一眼就知道差分约束的题目 但是最短路spfa的时候注意松弛条件是 if(dis[u]+w[i]<dis[v[i]]) dis[v[i]]=dis[u]+w[i]; 不能写成 if(dis[ ...

  6. [nginx] - 使用nginx实现反向代理,动静分离,负载均衡,session共享

    反向代理概念 先说正向代理,比如要访问youtube,但是不能直接访问,只能先找个FQ软件,通过FQ软件才能访问youtube. FQ软件就叫做正向代理.所谓的反向代理,指的是用户要访问youtube ...

  7. JZ2440存储管理器--SDRAM

     为了cpu访问外部设备,ARM提供一个存储管理器部件,提供访问外部设备的所需的信号(对SDRAM.网卡.nor等设备进行初始化,以便存储器管理器配合CPU进行与外设数据通讯).   CPU通常读写一 ...

  8. lvs笔记

    LVS是Linux Virtual Server的简写,意为Linux虚拟服务器,是虚拟的服务器集群系统,可在UNIX/LINUX平台下实现负载均衡集群功能.该项目在1998年5月由章文嵩博士组织成立 ...

  9. Angular CLI命令

    ng 基础命令 npm install –g @angular/cli npm install -g @angular/cli@latest ng serve –prot –aot 启动项目并压缩项目 ...

  10. 3、lvs调度方法详解

    3.lvs类型和调度方法详解    http://www.178linux.com/13570 集群:将多台主机组织起来满足某一特定需求: 集群类型: LB:Load Balancing, 负载均衡集 ...