C#本地上传文件到另一服务器

客户端代码

	/// 
    /// 客户端上传
    /// 
    /// 
    /// 
    public void ClientFileUpload(string FilePath)
    {
        try
        {
            //获取文件名
            string FileName = FilePath.Substring(FilePath.LastIndexOf("/") + 1);
            //获取文件上传的服务器地址
            string config = ConfigurationManager.AppSettings["SyncFilesPath"];  
            //接收文件Api
            string ServiceUrl = config + "api/Biz/CarQueue/FileUpload?" + FileName;
           
            HttpWebRequest request = (HttpWebRequest)WebRequest.Create(ServiceUrl);
            request.Method = "post";
            //读取文件
            FileStream fs = new FileStream(FilePath, FileMode.Open, FileAccess.Read);
            Stream requestStream = request.GetRequestStream();
            fs.CopyTo(requestStream);//适用于 .net 4.0以上
            // CopyTo(requestStream,fs);//适用 .net 2.0
            fs.Dispose();

            requestStream.Dispose();
            WebResponse result = request.GetResponse();
            Stream responseStream = result.GetResponseStream();

        }
        catch (Exception e)
        {
            ErrorLogTxt(DateTime.Now + ":" + e.Message);
        }
    }

4.0以下版本重写CpypTo方法

 	/// 
    /// 重写CpypTo方法
    /// 
    /// 
    /// 
public void CopyTo(Stream destination, FileStream fs)
    {
        byte[] array = new byte[fs.Length];
        int count;
        while ((count = fs.Read(array, 0, array.Length)) != 0)
        {
          destination.Write(array, 0, count);
        }
    }

服务端接收文件

 public class FileUploadService:ServiceBase<Response<string>, FileUpload>
        {
            public override Response<string> Execute(FileUpload request)
            {
                try
                {
                    var stream = HttpContext.Current.Request.InputStream;
                    if (stream.Length > 0)
                    {
                        string date = DateTime.Now.ToString("yyyyMMdd");
                        string filepath = HttpContext.Current.Server.MapPath("~/LIS/TempReports/" + date + "/");
                        string url = HttpContext.Current.Request.Url.ToString();
                        string Name = url.Substring(url.LastIndexOf("?") + 1);
                        var path = filepath + Name;
                        var folder = path.Substring(0, path.LastIndexOf("\\"));
                        if (!Directory.Exists(folder))//判断路径是否存在
                        {
                            Directory.CreateDirectory(folder);//创建路径
                        }
                        var bytes = new byte[stream.Length];
                        stream.Read(bytes, 0, bytes.Length);
                        using (FileStream fs = new FileStream(path, FileMode.Create, FileAccess.Write))
                        {
                            fs.Write(bytes, 0, bytes.Length);
                        }
                    }
                }
                catch (Exception e)
                { 
                }
                return InvokeResult.Success<string>("上传成功!");
            }
        }

你可能感兴趣的:(后端,c#)