微信公众平台对接C#- 根据MediaID下载资源

 #region 根据mediaId下载资源
        /// <summary>
        /// 根据mediaId下载资源
        /// </summary>
        /// <param name="dicRequestData"></param>
        /// <param name="guid"></param>
        /// <param name="context"></param>
        public static string DownLoadFileByMediaId(mdl.wx_account accountModel, string mediaId, string filetype, string FileEx
            , ref int sysFileId, ref int voicecount, string FileRoot, string guid, string LogName, HttpContext context)
        {
            try
            {
                #region 下载图片
                string accessToken = getAccessToken(accountModel.WXA_AppId, accountModel.WXA_Secret, accountModel.WXA_OtherWSURL, LogName, null, guid);
                string url2 = "https://file.api.weixin.qq.com/cgi-bin/media/get?access_token="
                      + accessToken + "&media_id=" + mediaId;
                T9.Util.LogUtil.WriteLog("https://file.api.weixin.qq.com/cgi-bin/media/get?access_token="
                      + accessToken + "&media_id=" + mediaId, guid, LogName);
                Byte[] bytes2 = T9.Util.HttpResponseUtil.SendRequestDownLoadByte(url2, "", LogName);
                sysFileId = 0;
                string filePath = "";
                sysFileId = SaveProFile(accountModel.WXA_ManagerID, 0, "WX_ReceiveMsg", "微信接收文件保存", filetype, FileEx, bytes2, FileRoot, ref filePath, ref voicecount, context, guid, LogName);
                T9.Util.LogUtil.WriteLog("DownLoadFileByMediaId-ID:" + sysFileId, guid, LogName);
                T9.Util.LogUtil.WriteLog("DownLoadFileByMediaId-filePath:" + filePath, guid, LogName);
                return filePath;
                #endregion
            }
            catch (Exception ex)
            {
                return "";
            }
        }
        #endregion

/// <summary>
        /// 保存文件(根据文件流)
        /// </summary>
        /// <param name="UID">用户ID</param>
        /// <param name="OID">所属文件的model.ID</param>
        /// <param name="OTable">所属的库名</param>
        /// <param name="ModuleName">模块名称</param>
        /// <param name="FileEx">文件的后缀名不包含(.)</param>
        /// <param name="strFileStreams">byte[]数组的文件流</param>
        /// <param name="FileRoot">文件跟目录</param>
        /// <param name="context"></param>
        /// <returns></returns>
        public static int SaveProFile(int UID, int OID, string OTable, string ModuleName, string filetype, string FileEx, byte[] strFileStreams, string FileRoot, ref string filepath, ref int voicecount, HttpContext context, string guid, string LogName)
        {
            DateTime nowTime = DateTime.Now;
            string UName = CKXD.WeiXinManage.BLL.sys_staff.GetNames(UID.ToString());
            //保存文件
            try
            {
                string FilePath = "";
                T9.Util.FileUtil.ExistsOrCreate(FilePath, FileRoot, context);
                FilePath += "/" + UName + "_" + UID;
                T9.Util.FileUtil.ExistsOrCreate(FilePath, FileRoot, context);
                FilePath += "/" + ModuleName;
                T9.Util.FileUtil.ExistsOrCreate(FilePath, FileRoot, context);
                FilePath += "/" + nowTime.ToString("yyyy-MM");
                T9.Util.FileUtil.ExistsOrCreate(FilePath, FileRoot, context);
                FilePath += "/" + nowTime.ToString("yyyy-MM-dd");
                T9.Util.FileUtil.ExistsOrCreate(FilePath, FileRoot, context);
                string FileName = Guid.NewGuid().ToString();
                int index = FileName.LastIndexOf('\\');
                if (index > -1)
                {
                    FileName = FileName.Substring(index + 1);
                }
                FilePath += "/" + FileName + "." + FileEx;
                if (filetype == "image")
                {
                    MemoryStream ms = new MemoryStream(strFileStreams);
                    System.Drawing.Bitmap bmp = new System.Drawing.Bitmap(ms);
                    bmp.Save(context.Server.MapPath(FileRoot + FilePath));
                    ms.Close();
                }
                else
                {
                    #region 生成amr文件
                    //生成amr文件  
                    File.WriteAllBytes(context.Server.MapPath(FileRoot + FilePath), strFileStreams);
                    T9.Util.LogUtil.WriteLog("SaveProFile---WriteAllBytes:", guid, LogName);
                    #endregion
                    if (filetype == "voice" && FileEx == "amr")
                    {
                        voicecount = strFileStreams.Length < 1024 ? 1 : (strFileStreams.Length / 1024 + (strFileStreams.Length % 1024 > 0 ? 1 : 0));
                        #region 语音格式转换
                        //转换mp3格式异常;
                        string mp3Voice = CommonVoid.ConvertToMp3(context, context.Server.MapPath(FileRoot + FilePath), context.Server.MapPath(FileRoot + FilePath).Replace(".amr", ".mp3"), guid, LogName);
                        T9.Util.LogUtil.WriteLog("SaveProFile-ConvertToMp3" + mp3Voice, guid, LogName);
                        FilePath = FilePath.Replace(".amr", ".mp3");
                        #endregion
                    }
                }
                //文件信息入库
                filepath = FilePath;
                try
                {
                    CKXD.WeiXinManage.Model.sys_files sysFile = new CKXD.WeiXinManage.Model.sys_files();
                    CKXD.WeiXinManage.BLL.sys_files sysFileBLL = new CKXD.WeiXinManage.BLL.sys_files();
                    sysFile.F_FilePath = FilePath;
                    sysFile.F_FileType = FileEx;
                    sysFile.F_Module = ModuleName;
                    sysFile.F_Table = OTable;
                    sysFile.F_ID = OID;
                    sysFile.F_FileIsExist = 0;
                    sysFile.UpdateTime = nowTime;
                    sysFile.UpdateUser = UID;
                    sysFile.CreateTime = nowTime;
                    sysFile.CreateUser = UID;
                    return sysFileBLL.Add(sysFile);
                }
                catch (Exception err)
                {
                    string logMsg = "";
                    logMsg += "报错页面:" + context.Request.Url.ToString();
                    logMsg += "\r\n报错信息:" + err.Message.ToString();
                    logMsg += "\r\n报错详情:" + err.StackTrace.ToString();
                    T9.Util.LogUtil.WriteLog(logMsg, "wxlog");
                    context.Server.ClearError();
                    File.Delete(context.Server.MapPath(FileRoot + FilePath));
                    return 0;
                }
            }
            catch (Exception err)
            {
                string logMsg = "";
                logMsg += "报错页面:" + context.Request.Url.ToString();
                logMsg += "\r\n报错信息:" + err.Message.ToString();
                logMsg += "\r\n报错详情:" + err.StackTrace.ToString();
                T9.Util.LogUtil.WriteLog(logMsg, "wxlog");
                context.Server.ClearError();
                return 0;
            }
        }

  • 0
    点赞
  • 0
    收藏
    觉得还不错? 一键收藏
  • 打赏
    打赏
  • 0
    评论

“相关推荐”对你有帮助么?

  • 非常没帮助
  • 没帮助
  • 一般
  • 有帮助
  • 非常有帮助
提交
评论
添加红包

请填写红包祝福语或标题

红包个数最小为10个

红包金额最低5元

当前余额3.43前往充值 >
需支付:10.00
成就一亿技术人!
领取后你会自动成为博主和红包主的粉丝 规则
hope_wisdom
发出的红包

打赏作者

可曾听闻丶

你的鼓励将是我创作的最大动力

¥1 ¥2 ¥4 ¥6 ¥10 ¥20
扫码支付:¥1
获取中
扫码支付

您的余额不足,请更换扫码支付或充值

打赏作者

实付
使用余额支付
点击重新获取
扫码支付
钱包余额 0

抵扣说明:

1.余额是钱包充值的虚拟货币,按照1:1的比例进行支付金额的抵扣。
2.余额无法直接购买下载,可以购买VIP、付费专栏及课程。

余额充值