Нет описания

UploadFileController.cs 8.8KB

123456789101112131415161718192021222324252627282930313233343536373839404142434445464748495051525354555657585960616263646566676869707172737475767778798081828384858687888990919293949596979899100101102103104105106107108109110111112113114115116117118119120121122123124125126127128129130131132133134135136137138139140141142143144145146147148149150151152153154155156157158159160161162163164165166167168169170171
  1. using CallCenter.Utility;
  2. using CallCenterApi.Interface.Controllers.Base;
  3. using System;
  4. using System.Collections.Generic;
  5. using System.Diagnostics;
  6. using System.IO;
  7. using System.Linq;
  8. using System.Text;
  9. using System.Threading;
  10. using System.Web;
  11. using System.Web.Mvc;
  12. namespace CallCenterApi.Interface.Controllers
  13. {
  14. public class UploadFileController : BaseController
  15. {
  16. // GET: UploadFile
  17. //public ActionResult Index()
  18. //{
  19. // return View();
  20. //}
  21. public ActionResult UploadToFTP()
  22. {
  23. ActionResult res = NoToken("未知错误,请重新登录");
  24. #region 前后端分离上传并修改文件格式
  25. HttpPostedFile _upFile = RequestString.GetFile("upFile");
  26. if (_upFile != null)
  27. {
  28. string datename = DateTime.Now.ToString("yyyyMMddHHMMss");
  29. string fullFileName = datename + "_" + _upFile.FileName;
  30. string ffmpegLocation = Server.MapPath("~/");
  31. string dirpath = Server.MapPath(this.Request.ApplicationPath + "\\auoupload");
  32. //string dirpath = Configs.GetValue("saveloc");
  33. if (!System.IO.Directory.Exists(dirpath))
  34. {
  35. System.IO.Directory.CreateDirectory(dirpath);
  36. }
  37. string path = this.Request.ApplicationPath + "/auoupload/" + fullFileName;
  38. //string physicalpath = dirpath + fullFileName;
  39. string physicalpath = Server.MapPath(path);
  40. _upFile.SaveAs(physicalpath);
  41. #region 修改音频格式
  42. if (!string.IsNullOrEmpty(physicalpath))
  43. {
  44. //通过cmd执行ffmpeg改变音频采样率
  45. Process p = new Process();
  46. p.StartInfo.FileName = "cmd.exe";
  47. p.StartInfo.UseShellExecute = false;
  48. p.StartInfo.RedirectStandardOutput = true;
  49. p.StartInfo.RedirectStandardInput = true;
  50. p.StartInfo.RedirectStandardError = true;
  51. p.StartInfo.CreateNoWindow = true;
  52. //p.StartInfo.Arguments = "ffmpeg -i "+fileName+ " -ar 16000 "+ namePart + "New.wav";//参数以空格分隔,如果某个参数为空,可以传入””
  53. //p.Start();
  54. ////先进入cmd命令
  55. ////再打开FFmpeg.exe
  56. ////p.StandardInput.WriteLine("exit");
  57. //p.StandardInput.WriteLine("cd d/ " + ffmpegLocation + "ffmpeg.exe");
  58. //p.StandardInput.WriteLine(ffmpegLocation.TrimEnd('\\'));
  59. string temp_filename = physicalpath.Split('\\').Last().Split('.')[0] + "_temp";
  60. string savedir = Configs.GetValue("saveloc");
  61. if (!System.IO.Directory.Exists(savedir))
  62. {
  63. System.IO.Directory.CreateDirectory(savedir);
  64. }
  65. p.Start();
  66. //先进入cmd命令
  67. //再打开FFmpeg.exe
  68. //p.StandardInput.WriteLine("exit");
  69. //cd d/ D:\项目\公司5.1\codegit\CallCenterApi\CallCenterApi.Interface\CallCenterApi.Interface\ffmpeg.exe
  70. p.StandardInput.WriteLine("cd " + ffmpegLocation);
  71. p.StandardInput.WriteLine(ffmpegLocation.Split('\\').First());
  72. p.StandardInput.WriteLine("ffmpeg.exe");
  73. //p.StandardInput.WriteLine(ffmpegLocation.TrimEnd('\\'));
  74. //p.StandardInput.WriteLine("ffmpeg -i " + temp_filename + i + ".mp3 -f pcm -ar 16000 -ac 1 -ab 16 " + namepart.Replace(@"/", @"\"));
  75. //string changefile = "ffmpeg -i " + physicalpath + " -acodec pcm_u8 -ab 64 -ar 8000 -ac 1 " + dirpath + "\\" + temp_filename + ".wav";
  76. string changefile = "ffmpeg -i " + physicalpath + " -acodec pcm_u8 -ab 64 -ar 8000 -ac 1 " + savedir + "\\" + temp_filename + ".wav";
  77. //ffmpeg -i D:\项目\公司5.1\codegit\CallCenterApi\CallCenterApi.Interface\CallCenterApi.Interface\upload\20180131150114_2566994_913583164044.wav -acodec pcm_s16le -f s16le -ar 8000 -ac 1 D:\项目\公司5.1\codegit\CallCenterApi\CallCenterApi.Interface\CallCenterApi.Interface\20180131150114_2566994_913583164044_temp.pcm
  78. p.StandardInput.WriteLine(changefile);
  79. Thread.Sleep(3000);
  80. p.StandardInput.WriteLine("exit");
  81. //Kill 是终止没有图形化界面的进程的唯一方法。
  82. //p.Kill();
  83. #region 上传
  84. uploadFile upfile = new uploadFile();
  85. //通过读取配置文件,获取数据库
  86. string _ftp = Configs.GetValue("ftp");
  87. string _acc = Configs.GetValue("account");
  88. string _pwd = Configs.GetValue("password");
  89. upfile.ftpPath = _ftp;
  90. upfile.ftpUserID = _acc;
  91. upfile.ftpPassword = _pwd;
  92. //FileInfo fi = new FileInfo(path);
  93. FileInfo fi = new FileInfo(physicalpath);
  94. //string res = upfile.UploadLocalToFtp(path);
  95. //string res = upfile.UploadLocalToFtp(physicalpath);
  96. //D:\项目\公司5.1\codegit\CallCenterApi\CallCenterApi.Interface\CallCenterApi.Interface\20180131150114_2566994_913583164044_temp.pcm
  97. //string uploadres = upfile.UploadLocalToFtp(dirpath + "\\" + temp_filename + ".wav");
  98. string uploadres = upfile.UploadLocalToFtp(savedir + "\\" + temp_filename + ".wav");
  99. if (uploadres == "上传成功!")
  100. {
  101. //写入日志
  102. string logfile = Server.MapPath("~/log.txt");
  103. if (!System.IO.File.Exists(logfile))
  104. {
  105. System.IO.File.Create(logfile);
  106. }
  107. //开始写入
  108. string str = DateTime.Now.ToString() + " 文件" + fullFileName + "上传成功";
  109. System.IO.File.AppendAllLines(logfile, new string[] { str }, Encoding.GetEncoding("gb2312"));
  110. //存入表中
  111. //string title = RequestString.GetFormString("title");
  112. //string content = RequestString.GetFormString("content");
  113. //string type = RequestString.GetFormString("type");
  114. //string stime = RequestString.GetFormString("stime");
  115. //string etime = RequestString.GetFormString("etime");
  116. //string remark = RequestString.GetFormString("remark");
  117. //string isstate = RequestString.GetFormString("isstate");
  118. string title = "123";
  119. string content = "123";
  120. string type = "1";
  121. string stime = "2018-01-30 17:27:57";
  122. string etime = "2018-01-30 17:27:57";
  123. string remark = "123";
  124. string isstate = "1";
  125. int userId = CurrentUser.UserData.F_UserId;
  126. string ucode = "";
  127. if (userId != 0)
  128. {
  129. Model.T_Sys_UserAccount ua = new BLL.T_Sys_UserAccount().GetModel(userId);
  130. if (ua != null)
  131. ucode = ua.F_UserCode;
  132. }
  133. new BLL.T_Sys_IVRWords().Add(new Model.T_Sys_IVRWords()
  134. {
  135. F_Title = title,
  136. F_Content = content,
  137. F_Type = Convert.ToInt32(type),
  138. F_StartDate = DateTime.Parse(stime),
  139. F_EndDate = DateTime.Parse(etime),
  140. F_Remark = remark,
  141. F_IsState = int.Parse(isstate),
  142. F_CreateTime = DateTime.Now,
  143. F_CreateUser = ucode,
  144. F_IsDel = 0,
  145. F_WavOldName = fullFileName,
  146. F_WavNewName = temp_filename + ".wav",
  147. F_WavPath = dirpath + "\\" + temp_filename + ".wav"
  148. });
  149. res = Success("上传成功");
  150. }
  151. #endregion
  152. }
  153. #endregion
  154. }
  155. else
  156. res = Error("参数传入失败");
  157. #endregion
  158. return res;
  159. }
  160. }
  161. }