webApi FTP mp3 转文件流在线播放

[HttpGet]
public async Task<HttpResponseMessage> GetCallRecordAudio(int id)
{
    var info = ModelOpretion.FirstOrDefault<BaseCallRecordInfo>(id);//查询对于的通话记录
    if (info.FID > 0)
    {
     //info.FRecordHttp FtpUrl
var bytes = await FileUpLoad.GetFileStreamAsync(info.FRecordHttp, ftpUser, ftpPwd); HttpResponseMessage response = new HttpResponseMessage(HttpStatusCode.OK);
    //response.Content = new StreamContent(stream) 通过流实例化的Conent文件为空 response.Content
= new ByteArrayContent(bytes); response.Content.Headers.ContentLength = bytes.Length; response.Content.Headers.ContentType = new MediaTypeHeaderValue("audio/mpeg"); response.Content.Headers.ContentDisposition = new ContentDispositionHeaderValue("inline") { FileName = $"{info.FCId}.mp3" }; return response; } else { return null; } } public static async Task<byte[]> GetFileStreamAsync(string ftpUrl, string ftpUser, string ftpPwd) { try { WebClient webClient = new WebClient(); webClient.Credentials = new NetworkCredential(ftpUser, ftpPwd); return await webClient.DownloadDataTaskAsync(new Uri(ftpUrl)); } catch { return null; } }

 

posted @ 2022-08-09 10:40  ibot  阅读(71)  评论(0)    收藏  举报