コード例 #1
0
        public ActionResult UploadImage()
        {
            //Response.Charset = "utf-8";
            //Response.ContentType = "text/javascript";
            int    typeId = DoRequest.GetFormInt("code");                      //图片分类编码
            string allow  = DoRequest.GetFormString("allow").ToLower().Trim(); //允许上传哪些后缀的图片,多个后缀使用逗号隔开,缺省为允许jpg/jpeg/gif/png/bmp
            //测试log
            //Logger.Log(DoRequest.GetFormString("name").ToLower().Trim());
            List <string> allowList = new List <string>();

            string[] arr       = allow.Split(',');
            string   _fullpath = "";

            foreach (string s in arr)
            {
                if (string.IsNullOrEmpty(s))
                {
                    continue;
                }
                string val = s.Trim();
                if (val.StartsWith("."))
                {
                    val = val.Substring(1);
                }
                allowList.Add(val);
            }

            if (!UpLoadFile.IsPostFile())
            {
                return(this.formatJson(Json(new { error = true, message = "请选择上传的文件" })));
            }
            var                returnValue = -1;
            Robots             robots      = null;
            HttpFileCollection postfiles   = System.Web.HttpContext.Current.Request.Files;

            if (postfiles[0] != null && postfiles[0].FileName != null && postfiles[0].FileName != "")
            {
                HttpPostedFile file = postfiles[0];
                try
                {
                    string ext = file.FileName.ToLower().Substring(file.FileName.LastIndexOf('.') + 1).ToLower();
                    if (ext == "jpg" || ext == "jpeg" || ext == "gif" || ext == "bmp" || ext == "png")
                    {
                    }
                    else
                    {
                        return(this.formatJson(Json(new { error = true, message = "不允许上传此类型文件" })));
                    }
                }
                catch (Exception e)
                {
                    Logger.Error(e.ToString());
                }
                NameValueCollection myCol = new NameValueCollection();
                robots = new Robots();
                //测试log
                //Logger.Log("进入上传");
                string s = robots.HttpUploadFile(config.UrlImagesUpload, file, myCol, "file");
                //测试log
                // Logger.Log("上传完毕");
                if (robots.IsError)
                {
                    return(this.formatJson(Json(new { error = true, message = robots.ErrorMsg })));
                }
                string uploadSaveMapPath = JsonHelper.JsonToObject <Response <ResponseFile> >(s).Body.fileNameList[0].fileKey;

                uploadSaveMapPath = uploadSaveMapPath.Replace("jianbao/website/", "");
                string savepath   = uploadSaveMapPath;
                string fullpathXX = JsonHelper.JsonToObject <Response <ResponseFile> >(s).Body.fileNameList[0].fileUrl;
                _fullpath = fullpathXX;
                #region 存储到数据表
                SysFilesInfo picfile = new SysFilesInfo();
                picfile.sp_type_id = typeId;//未分类
                picfile.file_name  = postfiles[0].FileName;
                picfile.save_name  = uploadSaveMapPath.Substring(uploadSaveMapPath.LastIndexOf("/") + 1);

                if (uploadSaveMapPath.StartsWith("/"))
                {
                    picfile.save_path = uploadSaveMapPath.Substring(1);
                }
                else
                {
                    picfile.save_path = uploadSaveMapPath;
                }
                picfile.user_agent = DoRequest.UserAgent;
                picfile.client_ip  = DoRequest.ClientIP;
                //测试log
                //Logger.Log("写入数据库");
                var res = AddSysPicFile.Do(picfile);
                //测试log
                //Logger.Log("写完数据库");
                if (res != null && res.Header != null && res.Header.Result != null && res.Header.Result.Code != null)
                {
                    returnValue = Utils.StrToInt(res.Header.Result.Code, -1);
                }
                #endregion
            }
            if (returnValue == 0)
            {
                return(this.formatJson(Json(new { error = false, message = "上传成功!", path = _fullpath })));
            }
            return(this.formatJson(Json(new { error = true, message = "上传失败..." })));
        }
コード例 #2
0
        public ActionResult UploadImage2()
        {
            HttpFileCollection postfiles = System.Web.HttpContext.Current.Request.Files;
            Robots             robots    = null;

            if (postfiles[0] != null && postfiles[0].FileName != null && postfiles[0].FileName != "")
            {
                string ext = postfiles[0].FileName.Substring(postfiles[0].FileName.LastIndexOf('.') + 1).ToLower();
                if (ext != "jpg" && ext != "jpeg" && ext != "gif" && ext != "bmp" && ext != "png")
                {
                    return(Json(new { error = true, message = "文件类型只能为jpg,jpeg,gif,bmp,png!" }, JsonRequestBehavior.AllowGet));
                }

                NameValueCollection myCol = new NameValueCollection();
                robots = new Robots();
                string s = robots.HttpUploadFile(config.UrlImagesUpload, postfiles[0], myCol, "file");
                if (robots.IsError)
                {
                    return(Json(new { error = true, message = robots.ErrorMsg }, JsonRequestBehavior.AllowGet));
                }

                ResponseFile file = JsonHelper.JsonToObject <Response <ResponseFile> >(s).Body;
                string       uploadSaveMapPath = JsonHelper.JsonToObject <Response <ResponseFile> >(s).Body.fileNameList[0].fileKey;

                uploadSaveMapPath = uploadSaveMapPath.Replace("jianbao/website/", "");
                string savepath   = uploadSaveMapPath;
                string fullpathXX = JsonHelper.JsonToObject <Response <ResponseFile> >(s).Body.fileNameList[0].fileUrl;

                var returnVal = -1;
                #region 存储到数据表
                SysFilesInfo picfile = new SysFilesInfo();
                picfile.sp_type_id = 1;//未分类
                picfile.file_name  = postfiles[0].FileName;
                picfile.save_name  = uploadSaveMapPath.Substring(uploadSaveMapPath.LastIndexOf("/") + 1);

                if (uploadSaveMapPath.StartsWith("/"))
                {
                    picfile.save_path = uploadSaveMapPath.Substring(1);
                }
                else
                {
                    picfile.save_path = uploadSaveMapPath;
                }
                picfile.user_agent = DoRequest.UserAgent;
                picfile.client_ip  = DoRequest.ClientIP;
                var res = AddSysPicFile.Do(picfile);
                if (res != null && res.Header != null && res.Header.Result != null && res.Header.Result.Code != null)
                {
                    returnVal = Utils.StrToInt(res.Header.Result.Code, -1);
                }
                #endregion
                if (returnVal == 0)
                {
                    uploadSaveMapPath = config.UrlImages + uploadSaveMapPath;
                    return(Json(new { error = false, message = "成功", save = savepath, image = uploadSaveMapPath, fullPath = fullpathXX }, JsonRequestBehavior.AllowGet));
                }
                else
                {
                    return(Json(new { error = true, message = "图片保存失败,请重新上传!" }, JsonRequestBehavior.AllowGet));
                }
            }
            else
            {
                return(Json(new { error = true, message = "失败,请重新选择文件!" }, JsonRequestBehavior.AllowGet));
            }
        }