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:FSTERPProductImage"); 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:FSTERPProductImage"); 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 }
测试图片
版权声明:本文为博主原创文章,未经博主允许不得转载。