加入收藏 | 设为首页 | 会员中心 | 我要投稿 李大同 (https://www.lidatong.com.cn/)- 科技、建站、经验、云计算、5G、大数据,站长网!
当前位置: 首页 > 综合聚焦 > 服务器 > 安全 > 正文

WebService上传图片

发布时间:2020-12-16 23:23:04 所属栏目:安全 来源:网络整理
导读:WebService端: [csharp] view plain copy print ? ///?summary ?? ???? ///?上传文件到远程服务器 ?? ???? ///?/summary ?? ???? ///?param?name="fileBytes"/param ?? ???? ///?param?name="fileName"/param ?? ???? ///?returns/returns? ?? ????[WebMeth

WebService端:

[csharp] view plain copy print ?
  1. ///?<summary>??
  2. ????///?上传文件到远程服务器??
  3. ????///?</summary>??
  4. ????///?<param?name="fileBytes"></param>??
  5. ????///?<param?name="fileName"></param>??
  6. ????///?<returns></returns>???
  7. ????[WebMethod(Description?=?"上传文件到远程服务器.")]??
  8. ????public?string?UploadFile(byte[]?fileBytes,?string?fileName)??
  9. ????{??
  10. ????????try??
  11. ????????{??
  12. ????????????MemoryStream?memoryStream?=?new?MemoryStream(fileBytes);?//1.定义并实例化一个内存流,以存放提交上来的字节数组。??
  13. ????????????FileStream?fileUpload?=?new?FileStream(Server.MapPath(".")?+?""?+?fileName,?FileMode.Create);?///2.定义实际文件对象,保存上载的文件。??
  14. ????????????memoryStream.WriteTo(fileUpload);?///3.把内存流里的数据写入物理文件??
  15. ????????????memoryStream.Close();??
  16. ????????????fileUpload.Close();??
  17. ????????????fileUpload?=?null;??
  18. ????????????memoryStream?=?null;??
  19. ????????????return?"文件已经上传成功";??
  20. ????????}??
  21. ????????catch?(Exception?ex)??
  22. ????????{??
  23. ????????????return?ex.Message;??
  24. ????????}??
  25. ????}??
客户端调用:

[csharp] view plain copy print ?
  1. private?void?BtnUpLoadImg_Click(object?sender,?EventArgs?e)??
  2. {??
  3. ????ImgService.ImgService?uf?=?new?ImgService.ImgService();??
  4. ????FileInfo?imgFile?=?new?FileInfo(@"D:我的图片拳皇人物1_110508104158_7.jpg");??
  5. ????byte[]?imgByte?=?new?byte[imgFile.Length];//1.初始化用于存放图片的字节数组??
  6. ????System.IO.FileStream?imgStream?=?imgFile.OpenRead();//2.初始化读取图片内容的文件流??
  7. ????imgStream.Read(imgByte,?0,?Convert.ToInt32(imgFile.Length));//3.将图片内容通过文件流读取到字节数组??
  8. ????MessageBox.Show(uf.UploadFile(imgByte,?"abc"?+?System.Guid.NewGuid()?+?".jpg"));//4.发送到服务器???
  9. }??

(编辑:李大同)

【声明】本站内容均来自网络,其相关言论仅代表作者个人观点,不代表本站立场。若无意侵犯到您的权利,请及时与联系站长删除相关内容!

    推荐文章
      热点阅读