private void UploadImage() { //var FileCollect = Request.Files[UploadConfig.UploadFieldName]; HttpFileCollection FileCollect = System.Web.HttpContext.Current.Request.Files; string uploadFileName = ""; int imgWidth, imgHeigth; string ext = string.Empty; if (FileCollect.Count > 0) { uploadFileName = FileCollect[domname].FileName; var size = FileCollect[domname].ContentLength; var fileStream = FileCollect[domname].InputStream; var byteData = new byte[size]; if (null != fileStream) { using (System.IO.BinaryReader br = new System.IO.BinaryReader(fileStream)) { byteData = br.ReadBytes(size); } }// 上传的文件为空 else { } if (uploadFileName.Contains(".")) { ext = uploadFileName.Split('.')[uploadFileName.Split('.').Length - 1]; } else { Utility.ImgHelper.IsImgageType(byteData, "jpg", out ext); } //不是图片格式不让上传 string[] ImgType = new string[] { "jpg", "jpeg", "gif", "png", "bmp" }; string[] VodType = new string[] { "mp4", "avi", "rmvb", "avi", "rm" }; if (ImgType.Contains <string>(ext.ToLower())) { Result.Url = uploadimg(byteData, out imgWidth, out imgHeigth, size, ext); } else if (VodType.Contains <string>(ext.ToLower())) { try { string aliTempImgKey = string.Empty; var aliTempImgFolder = AliOSSHelper.GetOssVoiceKey(ext.ToLower(), true, "video/folder", out aliTempImgKey); var putResult = AliOSSHelper.PutObjectFromByteArray(aliTempImgFolder, byteData, 1, "." + ext.ToLower()); Result.Url = aliTempImgKey; } catch (Exception ex) { log4net.LogHelper.WriteInfo(this.GetType(), ex.Message); } } } Result.OriginFileName = uploadFileName; Result.State = UploadState.Success; WriteResult(); }
public ActionResult UploadVoiceOnly() { try { var file = System.Web.HttpContext.Current.Request.Files[0]; if (file.InputStream.Length == 0) { return(Json(new { isok = false, msg = "上传失败,音频大小不能空", Path = "" })); } if ((file.InputStream.Length / 1024.0 / 1024.0) > 10) { return(Json(new { isok = false, msg = "上传失败,音频文件不能超过10M", Path = "" })); } if (!file.FileName.Substring(file.FileName.LastIndexOf(".")).Contains("mp3")) { return(Json(new { isok = false, msg = "上传失败,音频格式必须是MP3", Path = "" })); } byte[] byteData = new byte[file.InputStream.Length]; file.InputStream.Position = 0; file.InputStream.Read(byteData, 0, byteData.Length); file.InputStream.Close(); //将下载的语音放到AliOss临时文件夹 string voiceAliOssKey = string.Empty; //上传目录 string voiceAliOssFolder = AliOSSHelper.GetOssVoiceKey("mp3", true, "voice/folder", out voiceAliOssKey); bool putResult = AliOSSHelper.PutObjectFromByteArray(voiceAliOssFolder, byteData); if (!putResult)// 未能成功同步到AliOss { return(Json(new { isok = false, msg = "上传失败", Path = "" })); } return(Json(new { isok = true, msg = "上传成功", Path = voiceAliOssKey })); } catch (Exception) { return(Json(new { isok = false, msg = "系统错误,请重新尝试", Path = "" })); } }
//更新店铺时语音转mp3 public async Task <string> SendVoiceToAliOss(int itemId, int itemType, string sourceTempUrl) { if (itemId <= 0 || itemType <= 0 || string.IsNullOrEmpty(sourceTempUrl)) { return(string.Empty); } C_Attachment model = GetModelByType(itemId, itemType); if (model == null) { model = new C_Attachment(); model.itemId = itemId; model.itemType = itemType; model.createDate = DateTime.Now; model.thumbnail = ""; } model.filepath = sourceTempUrl; try { if (!model.filepath.Contains("temp"))//不是新上传的,停止操作! { return(string.Empty); } var bucket = ConfigurationManager.AppSettings["BucketName"]; int whichDomain = 1; string TemplateId = string.Empty; string PipelineId = string.Empty; TemplateId = ConfigurationManager.AppSettings["VoiceTemplateId"] ?? "42d5aac40e6a50bf13045a40aeb83b6f"; PipelineId = ConfigurationManager.AppSettings["PipelineId"] ?? "4bc9dd15cb3d48e39e0824e19c41defb"; var finalVoiceKey = string.Empty; var finalVoiceFolder = AliOSSHelper.GetOssVoiceKey("mp3", false, "voice/folder", out finalVoiceKey, whichDomain); //上传的本地音频。并且不是mp3|| 微信语音 if (!string.IsNullOrEmpty(model.VoiceServerId)) { //转换mp3 bool submitResult = await AliMTSHelper.SubmitJobs(model.filepath, finalVoiceKey, bucket, TemplateId, PipelineId, "", whichDomain); if (submitResult) {//转换成功。更新路径 model.thumbnail = finalVoiceKey; }//音频转换失败 else { log4net.LogHelper.WriteInfo(this.GetType(), "语音给AliOSS转换格式失败!ID为" + model.id + "==" + model.filepath); } }//mp3移动 else { //本地音频mp3格式从temp 拷贝 Task <bool> moveResult = Task <bool> .Factory.StartNew( () => { return(AliOSSHelper.CopyObect(model.filepath ?? "", finalVoiceKey)); } ); if (await moveResult) {//移动成功。更新路径 model.thumbnail = finalVoiceKey; } // 移动失败 else { log4net.LogHelper.WriteInfo(this.GetType(), "本地音频AliOSS临时文件夹移动到正式文件夹失败!ID为" + sourceTempUrl); return(""); } } if (model.id == 0) { Add(model); } else { MySqlParameter[] param = new MySqlParameter[] { new MySqlParameter("@itemId", itemId), new MySqlParameter("@filepath", model.filepath), new MySqlParameter("@thumbnail", model.thumbnail), new MySqlParameter("@id", model.id) }; string sql = "update C_Attachment set itemId=@itemId,filepath=@filepath,thumbnail=@thumbnail where id=@id"; SqlMySql.ExecuteNonQuery(Utility.dbEnum.MINIAPP.ToString(), System.Data.CommandType.Text, sql, param); RemoveRedis(model.itemId, model.itemType); } return(""); } catch (Exception ex) { log4net.LogHelper.WriteError(this.GetType(), new Exception("voiceid" + model.id + "移动语音失败:" + ex.Message)); return(string.Empty); } }
//更新店铺时语音转mp3 public async Task <string> SendVoiceToAliOss(int voiceId, int artId, int commentId, string content = "", bool needUpdateContent = false, bool isupdate = false) { string artcont = content; if (voiceId < 1) { return(string.Empty); } C_Attachment model = GetModel(voiceId); if (model == null) { return(string.Empty); } model.itemId = artId; try { if (!string.IsNullOrEmpty(model.filepath)) { if (isupdate) { if (!model.filepath.Contains("temp"))//不是新上传的,停止操作! { log4net.LogHelper.WriteInfo(this.GetType(), "观察日志:修改文章停止旧语音移动。文章ID=" + artId + "语音路径" + model.filepath); return(string.Empty); } } var bucket = ConfigurationManager.AppSettings["BucketName"]; int whichDomain = 1; string TemplateId = string.Empty; string PipelineId = string.Empty; TemplateId = ConfigurationManager.AppSettings["VoiceTemplateId"] ?? "42d5aac40e6a50bf13045a40aeb83b6f"; PipelineId = ConfigurationManager.AppSettings["PipelineId"] ?? "4bc9dd15cb3d48e39e0824e19c41defb"; var finalVoiceKey = string.Empty; var finalVoiceFolder = AliOSSHelper.GetOssVoiceKey("mp3", false, "voice/folder", out finalVoiceKey, whichDomain); //上传的本地音频。并且不是mp3|| 微信语音 if (!string.IsNullOrEmpty(model.VoiceServerId)) { //转换mp3 bool submitResult = await AliMTSHelper.SubmitJobs(model.filepath, finalVoiceKey, bucket, TemplateId, PipelineId, "", whichDomain); if (submitResult) {//转换成功。更新路径 model.thumbnail = finalVoiceKey; //图文混排内容里的音频替换 if (needUpdateContent) { var voiceurl = model.filepath; artcont = CRegex.Replace(artcont, voiceurl, finalVoiceKey, 0); } }//音频转换失败 else { log4net.LogHelper.WriteInfo(this.GetType(), "语音给AliOSS转换格式失败!ID为" + model.id + "==" + model.filepath); } }//mp3移动 else { //本地音频mp3格式从temp 拷贝 Task <bool> moveResult = Task <bool> .Factory.StartNew( () => { return(AliOSSHelper.CopyObect(model.thumbnail, finalVoiceKey)); } ); ; if (await moveResult) {//移动成功。更新路径 model.thumbnail = finalVoiceKey; //图文混排内容里的音频替换 if (needUpdateContent) { var voiceurl = model.filepath; artcont = CRegex.Replace(artcont, voiceurl, finalVoiceKey, 0); } } // 移动失败 else { log4net.LogHelper.WriteInfo(this.GetType(), "本地音频AliOSS临时文件夹移动到正式文件夹失败!ID为" + model.id); } } } MySqlParameter[] param = new MySqlParameter[] { new MySqlParameter("@itemId", artId), new MySqlParameter("@filepath", model.filepath), new MySqlParameter("@thumbnail", model.thumbnail), new MySqlParameter("@id", model.id) }; string sql = "update C_Attachment set itemId=@itemId,filepath=@filepath,thumbnail=@thumbnail where id=@id"; SqlMySql.ExecuteNonQuery(Utility.dbEnum.MINIAPP.ToString(), System.Data.CommandType.Text, sql, param); RedisUtil.Remove(string.Format(CImageKey, model.itemId, model.itemType)); RemoveRedis(model.itemId, model.itemType); return(artcont); } catch (Exception ex) { log4net.LogHelper.WriteError(this.GetType(), new Exception("voiceid" + model.id + "移动语音失败:" + ex.Message)); return(string.Empty); } }
public ActionResult UploadVoice() { try { var file = System.Web.HttpContext.Current.Request.Files[0]; if (file.InputStream.Length == 0) { return(Json(new { isok = false, msg = "上传失败,音频大小不能空", Path = "" }, JsonRequestBehavior.AllowGet)); } if ((file.InputStream.Length / 1024.0 / 1024.0) > 10) { return(Json(new { isok = false, msg = "上传失败,音频文件不能超过10M", Path = "" }, JsonRequestBehavior.AllowGet)); } if (!file.FileName.Substring(file.FileName.LastIndexOf(".")).Contains("mp3")) { return(Json(new { isok = false, msg = "上传失败,音频格式必须是MP3", Path = "" }, JsonRequestBehavior.AllowGet)); } int storeid = int.Parse(Context.GetRequest("storeid", "0")); int voicetype = Context.GetRequestInt("voicetype", (int)AttachmentItemType.小程序语音); byte[] byteData = new byte[file.InputStream.Length]; file.InputStream.Position = 0; file.InputStream.Read(byteData, 0, byteData.Length); file.InputStream.Close(); //将下载的语音放到AliOss临时文件夹 string voiceAliOssKey = string.Empty; //上传目录 string voiceAliOssFolder = AliOSSHelper.GetOssVoiceKey("mp3", true, "voice/folder", out voiceAliOssKey); bool putResult = AliOSSHelper.PutObjectFromByteArray(voiceAliOssFolder, byteData); if (!putResult)// 未能成功同步到AliOss { return(Json(new { isok = false, msg = "上传失败", Path = "" }, JsonRequestBehavior.AllowGet)); } List <C_Attachment> attachlist = C_AttachmentBLL.SingleModel.GetListByCache(storeid, voicetype); if (storeid > 0 && attachlist != null && attachlist.Count > 0) { C_AttachmentBLL.SingleModel.RemoveRedis(storeid, voicetype); return(Json(new { isok = true, msg = "上传成功", Path = voiceAliOssKey, voiceId = attachlist[0].id }, JsonRequestBehavior.AllowGet)); } else { C_Attachment attach = new C_Attachment(); attach.itemId = 0; attach.itemType = voicetype; attach.filepath = voiceAliOssKey; attach.createDate = DateTime.Now; attach.VoiceServerId = Guid.NewGuid().ToString(); attach.thumbnail = voiceAliOssKey; int voiceId = Convert.ToInt32(C_AttachmentBLL.SingleModel.Add(attach)); if (voiceId > 0) { return(Json(new { isok = true, msg = "上传成功", Path = voiceAliOssKey, voiceId = voiceId }, JsonRequestBehavior.AllowGet)); } } return(Json(new { isok = false, msg = "系统错误,请重新尝试", Path = "" })); } catch (Exception) { return(Json(new { isok = false, msg = "系统错误,请重新尝试", Path = "" })); } }
public void ProcessRequest(HttpContext context) { context.Response.ContentType = "text/plain"; string name = context.Request["name"]; string serverid = ""; var fid = Utility.IO.Context.GetRequestInt("minsnsId", 0); var accid = Utility.IO.Context.GetRequest("accountId", ""); int voicetime = 0, voicetype = 0, convertstate = 0; int.TryParse(context.Request["voicetime"], out voicetime); int.TryParse(context.Request["voicetype"], out voicetype); if (voicetype == 1) //1录音文件 0本地音频 { serverid = "app_" + DateTime.Now.ToString(); //前端显示需要用到serverid convertstate = 1; // 前端显示需要用到 标识已转换成功 } Guid _accountId = Guid.Empty; try { if (string.IsNullOrEmpty(accid)) { accid = CookieHelper.GetCookie("UserCookieNew"); } _accountId = Guid.Parse(accid); } catch (Exception) { log4net.LogHelper.WriteInfo(this.GetType(), "guid转换失败,上传音频_accountId=" + accid); context.Response.Write(new JavaScriptSerializer().Serialize(new { result = 0, msg = "账号信息错误!" })); } /*如果有openId,发帖人用微信帐号 xiaowei 2015-10-27 11:21:54*/ Account account = AccountBLL.SingleModel.GetModel(_accountId); if (null == account) { context.Response.Write(new JavaScriptSerializer().Serialize(new { result = 0, msg = "账号信息错误!" })); } OAuthUser artUser = null; int userid = 0; if (null != account && !string.IsNullOrEmpty(account.OpenId)) { artUser = new OAuthUserBll(fid).GetUserByOpenId(account.OpenId, fid); userid = artUser.Id; } HttpPostedFile file = context.Request.Files[name]; string time = string.Empty; string fileType = System.IO.Path.GetExtension(file.FileName).ToLower(); List <string> extents = new List <string> { ".cda", ".wav", ".mp3", ".wma", ".ra", ".midi", ".ogg", ".ape", ".flac", ".aac", ".amr" }; if (!extents.Contains(fileType)) { context.Response.Write(new JavaScriptSerializer().Serialize(new { result = 0, msg = "音频格式不对" })); } int convertState = 1; var size = file.ContentLength; string aliTempImgKey = string.Empty; if (fileType != ".mp3") { convertState = -9; } var filePath = @"\\share3.vzan.cc\share\temp\pc" + DateTime.Now.ToFileTime().ToString() + fileType; file.SaveAs(filePath); var fileStream = file.InputStream; var byteData = new byte[size]; if (null != fileStream) { using (System.IO.BinaryReader br = new System.IO.BinaryReader(fileStream)) { byteData = br.ReadBytes(size); } //同步到AliOss //上传的目录 var aliTempImgFolder = AliOSSHelper.GetOssVoiceKey(fileType.Replace(".", ""), true, "voice/folder", out aliTempImgKey); var putResult = AliOSSHelper.PutObjectFromByteArray(aliTempImgFolder, byteData, 1, fileType); if (!putResult) { log4net.LogHelper.WriteInfo(this.GetType(), "语音同步到Ali失败!"); } }// 上传的文件为空 else { log4net.LogHelper.WriteInfo(this.GetType(), "fileStream为null!"); context.Response.Write(new JavaScriptSerializer().Serialize(new { error = aliTempImgKey, msg = "上传失败" })); } Voices voice = new Voices(); voice.ServerId = ""; voice.MessageText = ""; voice.DownLoadFile = aliTempImgKey; voice.TransFilePath = aliTempImgKey; voice.UserId = userid; voice.FId = fid; voice.VoiceTime = voicetime; voice.VoiceType = voicetype; voice.ServerId = serverid; //录音文件 voice.ConvertState = convertstate; //1 已转换 0为转换 voice.CreateDate = DateTime.Now; if (convertState == 1)//是mp3--获取音频信息必须保存到本地才能获得 { HostFile.GetVoiceFromPath(ref voice, filePath); } else { if (File.Exists(filePath)) { try { File.Delete(filePath); } catch (Exception) { } } } if (string.IsNullOrEmpty(voice.SongName)) { voice.SongName = file.FileName; } if (string.IsNullOrEmpty(voice.SongPic)) { voice.SongPic = "//j.vzan.cc/manager/images/yinping.jpg?v=0.1"; } int voiceId = Convert.ToInt32(VoicesBll.SingleModel.Add(voice)); if (voiceId > 0) { context.Response.Write(new JavaScriptSerializer().Serialize(new { result = 1, msg = "上传成功", time = voice.VoiceTime, url = aliTempImgKey, songpic = voice.SongPic, songname = voice.SongName, singer = voice.Singer, album = voice.Album, id = voiceId, createdate = voice.CreateDate.ToString("yyyy-MM-dd") })); } else { context.Response.Write(new JavaScriptSerializer().Serialize(new { result = 0, msg = "上传错误" })); } }