webService上传图片

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

 

测试图片

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

 

转载于:https://www.cnblogs.com/qq260250932/p/4965982.html

本文来自互联网用户投稿,该文观点仅代表作者本人,不代表本站立场。本站仅提供信息存储空间服务,不拥有所有权,不承担相关法律责任。如若转载,请注明出处:http://www.mzph.cn/news/419316.shtml

如若内容造成侵权/违法违规/事实不符,请联系多彩编程网进行投诉反馈email:809451989@qq.com,一经查实,立即删除!

相关文章

在陌生Linux环境查看Tomcat服务的方法

1.查看Tomcat进程 执行命令$ps -ef|grep tomcat 你就能找出tomcat占据的进程号&#xff0c;当然这要求tomcat启动了。 # ps -ef | grep tomcat 2.查看Tomcat占据的端口 执行命令$netstat -nat能列出tomcat占据的端口&#xff0c;8080及其它类似的端口是需要注意的。这命令也需要…

查看WEB服务器的连接数

查看WEB服务器的连接数 https://technet.microsoft.com/en-us/sysinternals/bb897437 tcpView转载于:https://www.cnblogs.com/daishuguang/p/4973342.html

JVM系列三:JVM参数设置、分析

转载自&#xff1a; http://www.cnblogs.com/redcreen/archive/2011/05/04/2037057.html 不管是YGC还是Full GC,GC过程中都会对导致程序运行中中断,正确的选择不同的GC策略,调整JVM、GC的参数&#xff0c;可以极大的减少由于GC工作&#xff0c;而导致的程序运行中断方面的问题…

堆溢出,栈溢出

堆溢出:不断的new 一个对象&#xff0c;一直创建新的对象&#xff0c; 栈溢出&#xff1a;死循环或者是递归太深&#xff0c;递归的原因&#xff0c;可能太大&#xff0c;也可能没有终止。 在一次函数调用中&#xff0c;栈中将被依次压入&#xff1a;参数&#xff0c;返回地址&…

Information_Schema系统表

information_schema数据库是MySQL自带的&#xff0c;它提供了访问数据库元数据的方式。 元数据是关于数据的数据&#xff0c;如数据库名或表名&#xff0c;列的数据类型&#xff0c;或访问权限等。有些时候用于表述该信息的其他术语包括“数据词典”和“系统目录”。 在…

天赋是牛人的基因?

最近听了吴瀚清&#xff08;俗称道哥&#xff09;分享安全&#xff0c;他是西安交大少年班毕业&#xff0c;专注于安全技术领域&#xff0c;23 岁加入阿里巴巴成为集团最年轻的高级技术专家&#xff0c;目前在阿里任 P10 职级。对于这类少年天才&#xff0c;一般会冠与神童的美…

最长回文Manacher

预处理: 判断一个串是不是回文串&#xff0c;往往要分开编写&#xff0c;造成代码的拖沓 int LongestPalindrome(const char * s, int n){ int i, j, max; if (s 0 || n < 1) return 0; max 0; for (i 0; i < n; i){//i is the middle point of palindrome for (j 0;…

链表面试笔试题目总结

链表是最基本的数据结构&#xff0c;凡是学计算机的必须的掌握的&#xff0c;在面试的时候经常被问到&#xff0c;关于链表的实现&#xff0c;百度一下就知道了。在此可以讨论一下与链表相关的练习题。 1、在单链表上插入一个元素&#xff0c;要求时间复杂度为O(1) 解答&#x…

linux下常用FTP命令

linux下常用FTP命令 1. 连接ftp服务器 1. 连接ftp服务器格式&#xff1a;ftp [hostname| ip-address]a)在linux命令行下输入&#xff1a; ftp 192.168.1.1b)服务器询问你用户名和密码&#xff0c;分别输入用户名和相应密码&#xff0c;待认证通过即可。 2. 下载文件 下载文件通…

线程、多线程与线程池

线程&#xff1a;进程中负责程序执行的执行单元。一个进程中至少有一个线程。 多线程&#xff1a;解决多任务同时执行的需求&#xff0c;合理使用CPU资源。多线程的运行是根据CPU切换完成&#xff0c;如何切换由CPU决定&#xff0c;因此多线程运行具有不确定性。 线程池&…

selenium webdriver python 元素操作

常用操作 click 点击某个元素driver.find_element_by_id(“su”).click()cleardriver.find_element_by_id(“kw”).clear()send_keys模拟键盘输入driver.find_element_by_id(“kw”).send_keys(“hello”)from selenium.webdriver.common.keys import Keysdriver.find_element_…

通过mvn dependency:tree 查看依赖树,解决依赖jar冲突问题

举例&#xff1a;E:\01workspace\chenxh\09research\rop\rop>mvn dependency:tree [WARNING] [WARNING] Some problems were encountered while building the effective settings [WARNING] pluginRepositories.pluginRepository.id must not be local, this identifier is …