public async Task OnCompletedSpeechAsync() { var culture = CultureInfo.InvariantCulture; var timestamp = StartTime.ToString("s", culture); var audioFileName = $"{ConversationId}/{ConversationId}-{culture.TextInfo.ToLower(SpeakerType.ToString())}-{timestamp}.wav"; var audioFileResult = new AudioFileResult() { AudioFileName = audioFileName, AudioFileUrl = audioFileName, SegmentResults = listSegment.ToArray() }; var json = new RootObject() { AudioFileResults = new AudioFileResult[] { audioFileResult } }.ToJson(); var localDirectory = Environment.GetEnvironmentVariable("LocalAppData"); var outFilePath = Path.Combine(localDirectory, $"{Guid.NewGuid()}.json"); try { File.WriteAllText(outFilePath, json); if (File.Exists(outFilePath)) { var blobName = $"{ConversationId}/{ConversationId}-{culture.TextInfo.ToLower(SpeakerType.ToString())}-{timestamp}.json"; await AzureStorageHelper.UploadTranscriptFileAsync(outFilePath, Config["Azure.Storage.ConnectionString"], Config["Azure.Storage.Container.Transcript"], blobName).ConfigureAwait(false); File.Delete(outFilePath); Logger.LogInformation($"Successfully uploaded transcript file for {ConversationId}:{SpeakerType.ToString()}."); } } catch (IOException ex) { Logger.LogError(ex, $"Issue when uploading (or deleting) transcript file for {ConversationId}:{SpeakerType.ToString()}."); }; }
/// <summary> /// Http方式下载文件 /// </summary> /// <param name="chapterid">章节编号</param> /// <param name="AudioFileResult">文件信息</param> /// <param name="localPath">本地文件夹</param> /// <returns></returns> static bool Download(string chapterid, AudioFileResult audioInfo, string localPath) { string netUrl = ""; if (audioInfo.ourl.Trim().Length > 0) { netUrl = audioInfo.ourl; } else if (audioInfo.plink.Trim().Length > 0) { netUrl = audioInfo.plink; } else { netUrl = audioInfo.url; } bool flag = false; long startPosition = 0; // 上次下载的文件起始位置 FileStream writeStream = null; // 写入本地文件流对象 var file = new System.IO.FileInfo(netUrl); string newFileName = chapterid + file.Extension; string newFilePath = localPath + newFileName; // 判断要下载的文件夹是否存在 if (!File.Exists(newFilePath)) { writeStream = new FileStream(newFilePath, FileMode.Create);// 文件不保存创建一个文件 startPosition = 0; } else { Console.WriteLine(newFileName + " 文件已存在,跳过."); return(false); } try { HttpWebRequest myRequest = (HttpWebRequest)HttpWebRequest.Create(netUrl);// 打开网络连接 if (startPosition > 0) { myRequest.AddRange((int)startPosition);// 设置Range值,与上面的writeStream.Seek用意相同,是为了定义远程文件读取位置 } WebResponse res = myRequest.GetResponse(); long allSize = res.ContentLength; //总长度 Stream readStream = res.GetResponseStream(); // 向服务器请求,获得服务器的回应数据流 byte[] btArray = new byte[512]; // 定义一个字节数据,用来向readStream读取内容和向writeStream写入内容 int contentSize = readStream.Read(btArray, 0, btArray.Length); // 向远程文件读第一次 ProgressBar progressBar = new ProgressBar(Console.CursorLeft, Console.CursorTop, 50, ProgressBarType.Multicolor); var meicishuliang = 100 * 1.0 / (allSize * 1.0 / contentSize); var progressValue = 0.0; while (contentSize > 0) // 如果读取长度大于零则继续读 { writeStream.Write(btArray, 0, contentSize); // 写入本地文件 contentSize = readStream.Read(btArray, 0, btArray.Length); // 继续向远程文件读取 progressValue += meicishuliang; progressBar.Dispaly(Convert.ToInt32(progressValue)); } Console.WriteLine(); //关闭流 writeStream.Close(); readStream.Close(); flag = true; //返回true下载成功 } catch (Exception) { writeStream.Close(); flag = false; //返回false下载失败 } return(flag); }