webService上传图片
生活随笔
收集整理的這篇文章主要介紹了
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 }
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
總結
以上是生活随笔為你收集整理的webService上传图片的全部內容,希望文章能夠幫你解決所遇到的問題。
- 上一篇: C标准时间与时间戳的相互转换
- 下一篇: FOC:【2】SVPWM(七段式)的Ve