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

webservice下载文件

发布时间:2020-12-17 00:55:22 所属栏目:安全 来源:网络整理
导读:WebService项目中的Web.config配置代码 ??? appSettings ??????? add key="UploadFileFolder" value="/Uploads/TestUpload/" / ??? /appSettings WebService项目中的ImageService.asmx代码 ??????? /// summary ??????? /// Webservice中的下载文件处理函数

WebService项目中的Web.config配置代码

??? <appSettings>
??????? <add key="UploadFileFolder" value="/Uploads/TestUpload/" />
??? </appSettings>

WebService项目中的ImageService.asmx代码
??????? /// <summary>
??????? /// Webservice中的下载文件处理函数
??????? /// </summary>
??????? /// <param name="filePath">文件路径</param>
??????? /// <returns>返回文件流</returns>
??????? [WebMethod(Description = "下载服务器站点文件,传递文件相对路径")]
??????? public byte[] DownloadFile(string strFilePath)
??????? {
??????????? FileStream fs = null;
??????????? string CurrentUploadFolderPath = Server.MapPath(ConfigurationManager.AppSettings["UploadFileFolder"]);
??????????? string CurrentUploadFilePath = CurrentUploadFolderPath + strFilePath;
??????????? if (File.Exists(CurrentUploadFilePath))
??????????? {
??????????????? try
??????????????? {
??????????????????? ///打开现有文件以进行读取。
??????????????????? fs = File.OpenRead(CurrentUploadFilePath);
??????????????????? int b1;
??????????????????? System.IO.MemoryStream tempStream = new System.IO.MemoryStream();
??????????????????? while ((b1 = fs.ReadByte()) != -1)
??????????????????? {
??????????????????????? tempStream.WriteByte(((byte)b1));
??????????????????? }
??????????????????? return tempStream.ToArray();
??????????????? }
??????????????? catch (Exception ex)
??????????????? {
??????????????????? return new byte[0];
??????????????? }
??????????????? finally
??????????????? {
??????????????????? fs.Close();
??????????????? }
??????????? }
??????????? else
??????????? {
??????????????? return new byte[0];
??????????? }
??????? }

Winform项目中的窗体下载按钮代码
??????? private void btnDownload_Click(object sender,EventArgs e)
??????? {
??????????? string CurrentServiceFilePath = this.txtServiceFile.Text.Trim();
??????????? string CurrentDownloadFolderPath = this.txtDownloadFolder.Text.Trim();

??????????? if (CurrentServiceFilePath == "" || CurrentDownloadFolderPath == "")
??????????? {
??????????????? MessageBox.Show(DownloadImage(CurrentServiceFilePath,CurrentDownloadFolderPath));???? ?
??????????? }
??????????? else if (CurrentServiceFilePath == "")
??????????? {
??????????????? MessageBox.Show("请填写要下载的服务器文件路径和选择本地保存目录");
??????????? }
??????????? else if (CurrentDownloadFolderPath == "")
??????????? {

??????????????? MessageBox.Show("请填写要下载的服务器文件路径和选择本地保存目录");
??????? ?
??????????? }
??????? }

Winform项目中的窗体下载按钮调用函数

??????? /// <summary> ??????? /// 通过WebService下载文件 ??????? /// </summary> ??????? /// <param name="ServiceFilePath">服务器图片路径</param> ??????? /// <param name="DownloadFolderPath">本地图片路径</param> ??????? private string DownloadImage(string ServiceFilePath,string DownloadFolderPath) ??????? { ??????????? try ??????????? { ??????????????? string DownloadFileName=""; ??????????????? if (ServiceFilePath.Contains("/")) ??????????????? { ??????????????????? DownloadFileName=ServiceFilePath.Substring(ServiceFilePath.LastIndexOf("/")); ??????????????? } ??????????????? else ??????????????? { ??????????????????? DownloadFileName = ServiceFilePath; ??????????????? } ??????????????? string DownloadFilePath = DownloadFolderPath +""+ DownloadFileName; ??????????????? localhost.ImageService myImageService=new localhost.ImageService(); ??????????????? byte[] bytes = myImageService.DownloadFile(ServiceFilePath); ??????????????? if (bytes != null) ??????????????? { ??????????????????? if (!Directory.Exists(DownloadFolderPath)) {? ??????????????????????? Directory.CreateDirectory(DownloadFolderPath); ??????????????????? } ??????????????????? if (!File.Exists(DownloadFilePath)) ??????????????????? { ??????????????????????? File.Create(DownloadFilePath).Dispose(); ??????????????????? } ??????????????????? //如果不存在完整的上传路径就创建 ??????????????????? FileInfo downloadInfo = new FileInfo(DownloadFilePath); ??????????????????? if (downloadInfo.IsReadOnly) { downloadInfo.IsReadOnly = false; } ??????????????????? //定义并实例化一个内存流,以存放提交上来的字节数组。 ??????????????????? MemoryStream ms = new MemoryStream(bytes); ??????????????????? //定义实际文件对象,保存上载的文件。 ??????????????????? FileStream fs = new FileStream(DownloadFilePath,FileMode.Create); ??????????????????? ///把内内存里的数据写入物理文件 ??????????????????? ms.WriteTo(fs); ??????????????????? fs.Flush(); ??????????????????? ms.Flush(); ??????????????????? ms.Close(); ??????????????????? fs.Close(); ??????????????????? fs = null; ??????????????????? ms = null; ??????????????? } ??????????????? return "下载成功"; ??????????? } ??????????? catch(Exception ex) ??????????? { ??????????????? return "下载失败"+ex.Message; ??????????? } ??????? }

(编辑:李大同)

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

    推荐文章
      热点阅读