public ActionResult UploadAudioFile() { try { var file = Request.Files[0]; var strServerFilePath = Server.MapPath("/UploadFile/Audio/"); if (!Directory.Exists(strServerFilePath)) { Directory.CreateDirectory(strServerFilePath); } int fileSize = file.ContentLength; //得到文件大小 string extensionName = Path.GetExtension(file.FileName); //得到扩展名 string fileName = Guid.NewGuid().ToString(); string fullPath = Path.Combine(strServerFilePath, fileName + extensionName); file.SaveAs(fullPath); Duration duration = new ByShell32(); var result = duration.GetDuration(fullPath); return(Json(new { status = true, fileName = "/UploadFile/Audio/" + fileName + extensionName, duration = result.Item1 })); } catch (Exception ex) { Logger.FileLoggerHelper.WriteErrorLog("upload Audio file error-" + ex.Message); throw; } }
public async Task <ResponseResultDto <UploadFileInfo> > UploadAudioFile() { UploadFileInfo info = new UploadFileInfo(); if (!Request.Content.IsMimeMultipartContent()) { throw new HttpResponseException(HttpStatusCode.UnsupportedMediaType); } string root = HostingEnvironment.MapPath("/UploadFile/Audio/"); if (!Directory.Exists(root)) { Directory.CreateDirectory(root); } var provider = new MultipartFormDataMemoryStreamProvider(); try { await Request.Content.ReadAsMultipartAsync(provider); var item = provider.Contents[0]; //foreach (var item in provider.Contents) //{ string fileNameStr = item.Headers.ContentDisposition.FileName.Trim('"'); var arr = fileNameStr.Split('.'); string extensionName = arr[arr.Length - 1]; //得到扩展名 string fileName = Guid.NewGuid().ToString(); string fullPath = Path.Combine(root, fileName + "." + extensionName); var ms = item.ReadAsStreamAsync().Result; using (var br = new BinaryReader(ms)) { var data = br.ReadBytes((int)ms.Length); File.WriteAllBytes(fullPath, data); } //} Duration duration = new ByShell32(); var result = duration.GetDuration(fullPath); FileInfo fileInfo = new FileInfo(fullPath); info = new UploadFileInfo { FilePath = "/UploadFile/Audio/" + fileName + "." + extensionName, Duration = result.Item1, FileSize = fileInfo.Length }; } catch (Exception ex) { //Logger.FileLoggerHelper.WriteErrorLog(DateTime.Now + ":上传音频文件错误-" + ex.Message); } return(new ResponseResultDto <UploadFileInfo> { IsSuccess = true, ErrorMessage = string.Empty, Result = info }); }
static void Main(string[] args) {//MySqlConnectionString Duration duration = new ByShell32(); var result = duration.GetDuration($@"E:\4229759733.mp3"); }