コード例 #1
0
        public UploadImgResult UploadImage(HttpPostedFileBase file)
        {
            UploadImgResult result = new UploadImgResult();

            try
            {
                string fileNameExt = (new FileInfo(file.FileName)).Extension;

                //string saveName = GetImageName() + fileNameExt;

                //获得要保存的文件路径
                String newFileName = NewFileName(fileNameExt);
                String ymd         = DateTime.Now.ToString("yyyyMMdd", DateTimeFormatInfo.InvariantInfo);
                FilePhysicalPath += ymd + "/";
                FileRelativePath += ymd + "/";
                String fileFullPath = FilePhysicalPath + newFileName;
                LogHelper.LogMsg(fileFullPath);
                if (!Directory.Exists(FilePhysicalPath))
                {
                    Directory.CreateDirectory(FilePhysicalPath);
                }
                file.SaveAs(fileFullPath);

                string relativeFileFullPath = FileRelativePath + newFileName;
                result.Result = 1;
                result.msg    = relativeFileFullPath;
            }
            catch (Exception ex)
            {
                result.Result = 3;
                result.msg    = ex.Message;
            }
            return(result);
        }
コード例 #2
0
        public virtual async Task <string> UploadArtImageAndGetMediaID(int mpid, string fileUrl, MpMessageType type)
        {
            var account = await _mpAccountAppService.Get(new Abp.Application.Services.Dto.EntityDto <int> {
                Id = mpid
            });

            var access_token = await _accessTokenContainer.TryGetAccessTokenAsync(account.AppId, account.AppSecret);

            UploadImgResult responseModel = null;
            var             fileName      = fileUrl.Substring(fileUrl.LastIndexOf("/") + 1);

            try
            {
                responseModel = JsonConvert.DeserializeObject <UploadImgResult>(await HttpHelper.HttpPost($"https://api.weixin.qq.com/cgi-bin/media/uploadimg?access_token={access_token}&type={type.ToString()}", fileUrl, fileName));
            }
            catch
            {
                access_token = await _accessTokenContainer.TryGetAccessTokenAsync(account.AppId, account.AppSecret, true);

                try
                {
                    responseModel = JsonConvert.DeserializeObject <UploadImgResult>(await HttpHelper.HttpPost($"https://api.weixin.qq.com/cgi-bin/media/uploadimg?access_token={access_token}&type={type.ToString()}", fileUrl, fileName));
                }
                catch (Exception e)
                {
                    throw new UserFriendlyException(e.Message);
                }
            }
            return(responseModel != null ? responseModel.url : "");
        }
コード例 #3
0
        public UploadImgResult UploadImg(string path)
        {
            UploadImgResult uploadImgResult = null;
            string          appId           = WxOperatorProvider.Provider.GetCurrent().AppId;

            uploadImgResult = MediaApi.UploadImg(appId, path);
            return(uploadImgResult);
        }
コード例 #4
0
        /// <summary>
        /// 系统文件保存
        /// </summary>
        /// <param name="command"></param>
        /// <returns></returns>
        public UploadImgResult SaveSystemImg(SystemImgFileCommand command)
        {
            var result  = new UploadImgResult();
            var request = command.Data;

            if (request.ContentLength > 0)
            {
                // 上传用户图片
                var targetCategory = SystemUploadPath + @"/";
                targetCategory += GetSaveFile(UserFileType.Image) + @"/";
                var option = new FileAttachOption();

                if (command.SmallImgFileSetting.IsUser)
                {
                    option.OriginalImgWidth  = command.SmallImgFileSetting.Width;
                    option.OriginalImgHeight = command.SmallImgFileSetting.Height;
                    option.BuildOriginalImg  = true;
                    option.OriginalImgModel  = command.SmallImgFileSetting.ImgModel;
                }
                else
                {
                    option.BuildOriginalImg = false;
                }

                if (command.StandardImgFileSetting.IsUser)
                {
                    option.StandardImgWidth  = command.StandardImgFileSetting.Width;
                    option.StandardImgHeight = command.StandardImgFileSetting.Height;
                    option.BuildStandardImg  = true;
                    option.StandardImgModel  = command.StandardImgFileSetting.ImgModel;
                }
                else
                {
                    option.BuildStandardImg = false;
                }

                // 允许上传文件后缀
                var fullFilePath = _fileAttachmentUtility.SaveLocalhostImgAttach(request, targetCategory, option);
                if (command.SmallImgFileSetting.IsUser)
                {
                    result.SmallImg = targetCategory + "s_" + fullFilePath;
                }
                if (command.StandardImgFileSetting.IsUser)
                {
                    result.StandardImg = targetCategory + "b_" + fullFilePath;
                }

                result.Img = targetCategory + fullFilePath;
            }
            return(result);
        }
コード例 #5
0
        public JsonResult UploadImg(UploadImgParam param)
        {
            UploadImgResult res  = new UploadImgResult();
            string          name = "";

            try
            {
                if (param.file != null)
                {
                    Img    img   = new Img();
                    string path  = "~/Content/images/";
                    string eName = Path.GetExtension(param.file.FileName);
                    name = param.file.FileName;
                    //登陆背景图片保存
                    if (param.ObjId == 1 && param.Type == ImgType.BackImg)
                    {
                        path += "loginback" + eName;
                    }
                    else if (param.Type == ImgType.HeadImg)
                    {
                        path += "himg/himg_" + param.ObjId + "_" + param.Type + eName;
                    }

                    img.ImgPath = path;
                    img.ObjId   = param.ObjId;
                    img.ImgType = (int)param.Type;
                    img.Id      = param.Id;
                    param.file.SaveAs(Server.MapPath(path));
                    res.ImgId = SysConfigLogic.SaveImg(img);
                    res.State = State.Success;
                    SaveUserLog(AuthUser.UserName + "上传了照片" + path, LogLevel.Info, AuthUser.LoginName, "UploadImg", "上传照片");
                    //更新用户头像
                    if (param.Type == ImgType.HeadImg)
                    {
                        AuthUser.HeadImgId = img.Id;
                        UserLogic.UpdateUserHeadImg(param.ObjId, img.Id);
                    }
                }
            }
            catch (Exception ex)
            {
                LogHelper.ErrorLog(ex);
                res.State   = State.Error;
                res.Message = "服务器错误";
                SaveUserLog(AuthUser.UserName + "上传照片" + name + "失败:" + ex.Message, LogLevel.Error, AuthUser.LoginName, "UploadImg", "上传照片");
            }
            return(Json(res));
        }
コード例 #6
0
        public ActionResult Upload(FormCollection form)
        {
            //接收上传的文件
            if (Request.Files.Count == 0)
            {
                //Request.Files.Count 文件数为0上传不成功
                return(View());
            }
            //上传的文件
            var file = Request.Files[0];

            if (file.ContentLength == 0)
            {
                //文件大小大(以字节为单位)为0时,做一些操作
                return(View());
            }
            else
            {
                //文件大小不为0
                HttpPostedFileBase uploadfile = Request.Files[0];
                int    pos = Request.Files[0].FileName.LastIndexOf('.');
                string ext = Request.Files[0].FileName.Substring(pos, Request.Files[0].FileName.Length - pos);
                //保存成自己的文件全路径,newfile就是你上传后保存的文件,
                string newFile = DateTime.Now.ToString("yyyyMMddHHmmss") + ext;
                string path    = Server.MapPath("/upload");
                if (!Directory.Exists(path))
                {
                    Directory.CreateDirectory(path);
                }
                path += "//" + newFile;
                uploadfile.SaveAs(path);

                #region 讲图片上传至微信服务器
                string url  = string.Format("http://file.api.weixin.qq.com/cgi-bin/media/uploadimg?access_token=" + weixinService.Access_token);
                string json = wxMediaService.HttpUploadFile(url, path);
                #endregion
                UploadImgResult um = JSONHelper.JSONToObject <UploadImgResult>(json);
                Response.Redirect(um.url);
                return(View());
                //JObject jb = (JObject)JsonConvert.DeserializeObject(json);//这里就能知道返回正确的消息了下面是个人的逻辑我就没写不用看省略的
            }
        }
コード例 #7
0
        public ActionResult Upload_poiimg(FormCollection form)
        {
            //接收上传的文件
            if (Request.Files.Count == 0)
            {
                //Request.Files.Count 文件数为0上传不成功
                return(View());
            }
            //上传的文件
            var file = Request.Files[0];

            if (file.ContentLength == 0)
            {
                //文件大小大(以字节为单位)为0时,做一些操作
                return(View());
            }
            else
            {
                //文件大小不为0
                HttpPostedFileBase uploadfile = Request.Files[0];
                int    pos = Request.Files[0].FileName.LastIndexOf('.');
                string ext = Request.Files[0].FileName.Substring(pos, Request.Files[0].FileName.Length - pos);
                //保存成自己的文件全路径,newfile就是你上传后保存的文件,
                string newFile = DateTime.Now.ToString("yyyyMMddHHmmss") + ext;
                string path    = Server.MapPath("/upload");
                if (!Directory.Exists(path))
                {
                    Directory.CreateDirectory(path);
                }
                path += "//" + newFile;
                uploadfile.SaveAs(path);

                #region 讲图片上传至微信服务器
                string url  = string.Format("http://file.api.weixin.qq.com/cgi-bin/media/uploadimg?access_token=" + weixinService.Access_token);
                string json = wxMediaService.HttpUploadFile(url, path);
                #endregion
                UploadImgResult ui = JSONHelper.JSONToObject <UploadImgResult>(json);
                ViewBag.imgurl = ui.url;
                return(View());
            }
            //{ "url":"http:\/\/mmbiz.qpic.cn\/mmbiz_jpg\/crL5YN9AycYvxK3y2plSTdhFpWHWfIOAqJUcQh740mwZzJK16SGuStYia44tcmosWiak3wfO3NjxQSZ5r9via1qKw\/0"}
        }
コード例 #8
0
        public string UploadPic()
        {
            Response.ContentType = "text/plain";
            List <UploadImgResult> results = new List <UploadImgResult>();

            FileRelativePath = System.Configuration.ConfigurationManager.AppSettings["UploadProductImgPath"];
            FilePhysicalPath = System.Web.HttpContext.Current.Server.MapPath(FileRelativePath);
            if (!Directory.Exists(FilePhysicalPath))
            {
                Directory.CreateDirectory(FilePhysicalPath);
            }

            string saveFileResult        = string.Empty;
            HttpFileCollectionBase files = (HttpFileCollectionBase)Request.Files;

            for (int i = 0; i < files.Count; i++)
            {
                HttpPostedFileBase file        = files[i];
                UploadImgResult    result      = null;
                string             checkResult = CheckImage(file);
                if (string.IsNullOrEmpty(checkResult))
                {
                    //result = UploadImage(file);
                }
                else
                {
                    result        = new UploadImgResult();
                    result.Result = 2;
                    result.msg    = checkResult;
                }
                results.Add(result);
            }
            string json = JsonConvert.SerializeObject(results);

            return(json);
        }