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)