Преглед на файлове

终极版 修改语音文件在服务器上传 文件不存在以及文件被占用等等问题

machenyang преди 8 години
родител
ревизия
d70a2e40c2

+ 17 - 5
codegit/CallCenterApi/CallCenterApi.Interface/CallCenterApi.Interface/Controllers/IVRWordsController.cs

@@ -287,7 +287,7 @@ namespace CallCenterApi.Interface.Controllers
287 287
                 }
288 288
                 #endregion
289 289
 
290
-                #region 保存原文件到本地指定目录
290
+                #region 保存原文件到项目中
291 291
                 string path = this.Request.ApplicationPath + "/auoupload/" + fullFileName;
292 292
                 if (!System.IO.Directory.Exists(Server.MapPath(this.Request.ApplicationPath + "/auoupload/")))
293 293
                 {
@@ -297,7 +297,7 @@ namespace CallCenterApi.Interface.Controllers
297 297
                 _upFile.SaveAs(physicalpath);
298 298
                 #endregion
299 299
 
300
-                #region 修改音频格式
300
+                #region 修改音频格式并保存新文件到指定目录中和项目中
301 301
                 if (!string.IsNullOrEmpty(physicalpath))
302 302
                 {
303 303
                     # region 修改音频格式
@@ -311,17 +311,29 @@ namespace CallCenterApi.Interface.Controllers
311 311
                     p.StartInfo.CreateNoWindow = true;
312 312
 
313 313
                     string temp_filename = physicalpath.Split('\\').Last().Split('.')[0] + "_temp";
314
+                    string path1 = this.Request.ApplicationPath + "/auoupload/";
315
+                    string physicalpath1 = Server.MapPath(path1);
314 316
 
315 317
                     p.Start();
316 318
                     p.StandardInput.WriteLine("cd " + ffmpegLocation);
317 319
                     p.StandardInput.WriteLine(ffmpegLocation.Split('\\').First());
318 320
                     p.StandardInput.WriteLine("ffmpeg.exe");
319 321
                     string changefile = "ffmpeg -i " + physicalpath + " -acodec pcm_u8 -ab 64 -ar 8000 -ac 1 " + savedir + temp_filename + ".wav";
322
+                    string changefile1 = "ffmpeg -i " + physicalpath + " -acodec pcm_u8 -ab 64 -ar 8000 -ac 1 " + physicalpath1 + temp_filename + ".wav";
320 323
                     p.StandardInput.WriteLine(changefile);
324
+                    p.StandardInput.WriteLine(changefile1);
325
+                    while (true)
326
+                    {
327
+                        if (System.IO.File.Exists(physicalpath1 + temp_filename + ".wav"))
328
+                            break;
329
+                    }
321 330
                     Thread.Sleep(3000);
322 331
                     p.StandardInput.WriteLine("exit");
332
+                    //p.WaitForExit();//等待程序执行完退出进程
333
+                    p.Close();//关闭进程
323 334
                     //Kill 是终止没有图形化界面的进程的唯一方法。
324
-                    p.Kill();
335
+                    p.Dispose();//释放资源
336
+                    //p.Kill();
325 337
                     #endregion
326 338
                     #region 上传
327 339
                     uploadFile upfile = new uploadFile();
@@ -334,7 +346,7 @@ namespace CallCenterApi.Interface.Controllers
334 346
                     upfile.ftpUserID = _acc;
335 347
                     upfile.ftpPassword = _pwd;
336 348
                     //20180207 原文件同时保存
337
-                    string uploadres = upfile.UploadLocalToFtp(savedir +  temp_filename + ".wav");
349
+                    string uploadres = upfile.UploadLocalToFtp(physicalpath1 + temp_filename + ".wav");
338 350
                     string uploadBeforres = upfile.UploadLocalToFtp(physicalpath);
339 351
                     if (uploadres == "上传成功!" && uploadBeforres == "上传成功!")
340 352
                     {
@@ -350,7 +362,7 @@ namespace CallCenterApi.Interface.Controllers
350 362
 
351 363
 
352 364
                         //删除本地文件
353
-                        System.IO.File.Delete(savedir + fullFileName);
365
+                        //System.IO.File.Delete(savedir + fullFileName);
354 366
                         System.IO.File.Delete(savedir + temp_filename + ".wav");
355 367
                         //System.IO.File.Delete(physicalpath);
356 368
                         var obj = new

+ 13 - 6
codegit/CallCenterCommon/CallCenter.Utility/uploadFile.cs

@@ -43,17 +43,24 @@ namespace CallCenter.Utility
43 43
             int contentLen;
44 44
 
45 45
             // 打开一个文件流 (System.IO.FileStream) 去读上传的文件  
46
-            FileStream fs = fileInf.OpenRead();
46
+            //FileStream fs = fileInf.OpenRead();
47
+            FileStream fs = new FileStream(localFile, FileMode.Open, FileAccess.Read, FileShare.ReadWrite);
47 48
             try
48 49
             {
49 50
                 res += uri+localFile;
50
-                Stream strm = reqFTP.GetRequestStream();// 把上传的文件写入流
51
+                //Stream strm = reqFTP.GetRequestStream();// 把上传的文件写入流
52
+                StreamReader strm = new StreamReader(fs, System.Text.Encoding.Default);
51 53
                 contentLen = fs.Read(buff, 0, buffLength);// 每次读文件流的2kb 
52
-                while (contentLen != 0)// 流内容没有结束  
54
+                //while (contentLen != 0)// 流内容没有结束  
55
+                //{
56
+                //    // 把内容从file stream 写入 upload stream  
57
+                //    strm.Write(buff, 0, contentLen);
58
+                //    contentLen = fs.Read(buff, 0, buffLength);
59
+                //}
60
+                StringBuilder sb = new StringBuilder();
61
+                while (!strm.EndOfStream)
53 62
                 {
54
-                    // 把内容从file stream 写入 upload stream  
55
-                    strm.Write(buff, 0, contentLen);
56
-                    contentLen = fs.Read(buff, 0, buffLength);
63
+                    sb.AppendLine(strm.ReadLine() + "<br>");
57 64
                 }
58 65
                 // 关闭两个流  
59 66
                 strm.Close();