解决方案:

1. 在目标服务器上发布webservice,实现文件下载的方法。

using System;
using System.Collections.Generic;
using System.Linq;
using System.Web;
using System.Web.Services;
using System.IO;
using System.Data;

namespace TRNWebService
{
    /// <summary>
    /// FileHelperService 的摘要说明
    /// </summary>
    [WebService(Namespace = "http://tempuri.org/")]
    [WebServiceBinding(ConformsTo = WsiProfiles.BasicProfile1_1)]
    [System.ComponentModel.ToolboxItem(false)]
    // 若要允许使用 ASP.NET AJAX 从脚本中调用此 Web 服务,请取消对下行的注释。
    // [System.Web.Script.Services.ScriptService]
    public class FileHelperService : System.Web.Services.WebService
    {
       
        /// <summary>
        /// 从指定文件夹下获取文件
        /// </summary>
        /// <param name="dicpath"></param>
        /// <returns></returns>
        [WebMethod(EnableSession = true, Description = "获取文件夹下面的文件")]
        public  DataTable GetFileList(string dicpath)
        {
            DataTable dt = new DataTable();
            dt.TableName = "AEFiles";
            dt.Columns.Add("fileName", typeof(string));
            dt.Columns.Add("filePath", typeof(string));
           

            DirectoryInfo dir = System.IO.Directory.CreateDirectory(dicpath);
            if (dir == null)
            {
                return null;
            }
            FileInfo[] files = dir.GetFiles();
            for (int i = 0; i < files.Length; i++)
            {
                FileInfo file = files[i] as FileInfo;
                if (file != null)
                {
                    dt.Rows.Add(file.Name, file.FullName);
                }
            }

            return dt;

        }



        [WebMethod(Description = "下载服务器站点文件,传递文件相对路径")]
        public byte[] DownloadFile(string strFilePath)
        {
            FileStream fs = null; 
            //string CurrentUploadFolderPath = Server.MapPath(System.Configuration.ConfigurationManager.AppSettings["UploadFileFolder"]);
            //string CurrentUploadFilePath = CurrentUploadFolderPath + strFilePath;
            string CurrentUploadFilePath = 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];
            }
        }
    }
}
webservice 下载文件

相关文章:

  • 2021-08-09
  • 2021-09-19
  • 2022-01-08
  • 2021-09-04
  • 2022-12-23
  • 2021-11-19
猜你喜欢
  • 2022-12-23
  • 2022-12-23
  • 2022-12-23
  • 2022-03-01
  • 2021-07-12
  • 2022-02-19
  • 2021-04-18
相关资源
相似解决方案