从服务器下载文件到本地或者到自己的服务器

public JsonResult GetPlayRecord(string FilePath, string FileName)
        {
            if (string.IsNullOrEmpty(FilePath))
                return JsonResultExt.Failed("文件不存在");
            Stream stream = null;

            string playPath = "";//前端播放地址
            string realPath = "";//服务器保存地址
            if (_options.Value.CallCompany == "ADJ")
            {
                 realPath = Path.Combine(Directory.GetCurrentDirectory(), "Files", "Attachments", FileName);//服务器保存地址
                FileName = HttpUtility.UrlEncode(FileName);//FileName.Replace("#", "%23");
                playPath = Path.Combine("Files", "Attachments", FileName);//前端播放地址
                //判断服务器文件存不存在
                if (!System.IO.File.Exists(realPath))
                {
                    //WebClient client = new WebClient();
                    string strUserName = _options.Value.CompanyADJ.FtpUserName;
                    string strPassword = _options.Value.CompanyADJ.FtpUserPassword;
                    //client.Credentials = new NetworkCredential(strUserName, strPassword);

                    string ftpAddress = _options.Value.CompanyADJ.FtpPath;//奥迪坚ftp地址

                    //录音文件路径
                    string RecordPath = Path.Combine(ftpAddress, FilePath.TrimStart('/'), FileName);

                    FtpWebRequest request = (FtpWebRequest)WebRequest.Create(RecordPath);
                    request.Credentials = new NetworkCredential(strUserName, strPassword);
                    //读取文件字节流
                    stream = request.GetResponse().GetResponseStream();

                }
            }
            else if (_options.Value.CallCompany == "LD")
            {
                string recordName = Path.GetFileName(FilePath);

                playPath = Path.Combine("Files", "Attachments", recordName);
                realPath = Path.Combine(Directory.GetCurrentDirectory(), "Files", "Attachments", recordName);//服务器保存地址

                //判断服务器文件存不存在
                if (!System.IO.File.Exists(realPath))
                {
                    string recordPath = Path.Combine(_options.Value.CompanyLD.PhoneRecordAddress, FilePath);
                    HttpWebRequest request = (HttpWebRequest)WebRequest.Create(recordPath);
                    WebResponse response = request.GetResponse();
                    stream = response.GetResponseStream();
                }
            }
            if (stream != null)
            {
                //字节流不为空则写入字节流
                using (FileStream fileStream = new FileStream(realPath, FileMode.Create))
                {
                    byte[] buffer = new byte[102400];
                    int read = 0;
                    do
                    {
                        read = stream.Read(buffer, 0, buffer.Length);
                        fileStream.Write(buffer, 0, read);
                        fileStream.Flush();
                    } while (!(read == 0));

                    fileStream.Flush();
                    fileStream.Close();
                }
            }

            return JsonResultExt.Success(new { FilePath = playPath });

        }

文件名或者路径中有特殊字符时

进行转义

1. + URL 中+号表示空格 %2B 
2. 空格 URL中的空格可以用+号或者编码 %20 
3. / 分隔目录和子目录 %2F 
4. ? 分隔实际的 URL 和参数 %3F 
5. % 指定特殊字符 %25 
6. # 表示书签 %23 
7. & URL 中指定的参数间的分隔符 %26 
8. = URL 中指定参数的值 %3D

或者用 HttpUtility.UrlEncode(str)

  • 0
    点赞
  • 0
    收藏
    觉得还不错? 一键收藏
  • 打赏
    打赏
  • 0
    评论

“相关推荐”对你有帮助么?

  • 非常没帮助
  • 没帮助
  • 一般
  • 有帮助
  • 非常有帮助
提交
评论
添加红包

请填写红包祝福语或标题

红包个数最小为10个

红包金额最低5元

当前余额3.43前往充值 >
需支付:10.00
成就一亿技术人!
领取后你会自动成为博主和红包主的粉丝 规则
hope_wisdom
发出的红包

打赏作者

雨中深巷的油纸伞

你的鼓励将是我创作的最大动力

¥1 ¥2 ¥4 ¥6 ¥10 ¥20
扫码支付:¥1
获取中
扫码支付

您的余额不足,请更换扫码支付或充值

打赏作者

实付
使用余额支付
点击重新获取
扫码支付
钱包余额 0

抵扣说明:

1.余额是钱包充值的虚拟货币,按照1:1的比例进行支付金额的抵扣。
2.余额无法直接购买下载,可以购买VIP、付费专栏及课程。

余额充值