Esempio n. 1
0
        /// <summary>
        /// 上传文件
        /// </summary>
        /// <param name="context"></param>
        /// <returns></returns>
        private string UploadSingelFile1(HttpContext context)
        {
            if (context.Request.Files.Count == 0)
            {
                resp.Status = 0;
                resp.Msg    = "请选择文件!";
                return(Common.JSONHelper.ObjectToJson(resp));
            }

            HttpPostedFile file = context.Request.Files[0];
            string         fd   = context.Request["fd"];//存储文件夹

            string fileExtension = Path.GetExtension(file.FileName).ToLower();
            string newFileName   = Guid.NewGuid().ToString("N").ToUpper() + Path.GetExtension(file.FileName).ToLower();

            if (fd.ToLower().Equals("music"))
            {
                if (!newFileName.EndsWith(".mp3"))
                {
                    resp.Status = 0;
                    resp.Msg    = "请上传mp3格式的音乐";
                    return(Common.JSONHelper.ObjectToJson(resp));
                }
            }
            else if (string.IsNullOrWhiteSpace(fd))
            {
                fd = "image";
            }
            if (!newFileName.EndsWith(".jpg") && !newFileName.EndsWith(".png") && !newFileName.EndsWith(".gif") && !newFileName.EndsWith(".mp3") && !newFileName.EndsWith(".jpeg"))
            {
                resp.Status = 0;
                resp.Msg    = "请上传JPG、PNG和GIF格式图片!";
                return(Common.JSONHelper.ObjectToJson(resp));
            }

            if (file != null)
            {
                try
                {
                    WebsiteInfo webSite = bllUser.GetWebsiteInfoModel();
                    if (ZentCloud.Common.ConfigHelper.GetConfigInt("Oss_Enable") == 0)
                    {
                        //文件保存目录路径
                        String savePath   = "/FileUpload/" + fd + "/" + currentUserInfo.WebsiteOwner + "/" + currentUserInfo.UserID + "/" + DateTime.Now.ToString("yyyyMMdd", System.Globalization.DateTimeFormatInfo.InvariantInfo) + "/";
                        String serverPath = context.Server.MapPath(savePath);
                        if (!Directory.Exists(serverPath))
                        {
                            Directory.CreateDirectory(serverPath);
                        }
                        String serverFile = serverPath + newFileName;
                        String saveFile   = savePath + newFileName;
                        if (!fileExtension.Equals(".mp3"))
                        {
                            ZentCloud.Common.ImgWatermarkHelper imgHelper = new ZentCloud.Common.ImgWatermarkHelper();
                            Image image = Image.FromStream(file.InputStream);
                            image.Save(serverFile, imgHelper.GetImageFormat(fileExtension));
                            resp.ExStr = saveFile;
                            resp.ExObj = new
                            {
                                width  = image.Width,
                                height = image.Height
                            };
                        }
                        else
                        {
                            file.SaveAs(serverFile);
                            resp.ExStr = saveFile;
                        }
                        resp.Status = 1;
                    }
                    else
                    {
                        resp.ExStr = bllUploadOtherServer.uploadFromHttpPostedFile(file);
                        //OssHelper.UploadFile(OssHelper.GetBucket(currentUserInfo.WebsiteOwner), OssHelper.GetBaseDir(currentUserInfo.WebsiteOwner), currentUserInfo.UserID, "image", file);

                        if (!fileExtension.Equals(".mp3"))
                        {
                            //获取图片宽高
                            try
                            {
                                Image image = Image.FromStream(file.InputStream);
                                resp.ExObj = new
                                {
                                    width  = image.Width,
                                    height = image.Height
                                };
                            }
                            catch (Exception)
                            {
                            }
                        }
                        resp.Status = 1;
                    }
                }
                catch (Exception ex)
                {
                    resp.Status = 0;
                    resp.Msg    = ex.Message;
                }
                //file.SaveAs(context.Server.MapPath("~/FileUpload/") + fd + "/" + newFileName);
                //resp.Status = 1;
                //resp.ExStr = "/FileUpload/" + fd + "/" + newFileName;
                //if (!newFileName.EndsWith(".mp3"))
                //{
                //    resp.ExStr = bllImage.CreateThumbImage(resp.ExStr,750,750);
                //}
            }
            else
            {
                resp.Status = 0;
                resp.Msg    = "找不到文件!";
            }
            return(Common.JSONHelper.ObjectToJson(resp));
        }
Esempio n. 2
0
        /// <summary>
        /// 上传文件
        /// </summary>
        /// <param name="context"></param>
        /// <returns></returns>
        private string Add(HttpContext context)
        {
            //定义允许上传的文件扩展名
            Hashtable extTable = new Hashtable();

            extTable.Add("image", "gif,jpg,jpeg,png,bmp");
            extTable.Add("flash", "swf,flv");
            extTable.Add("media", "swf,flv,mp3,wav,wma,wmv,mid,avi,mpg,asf,rm,rmvb");
            extTable.Add("file", "doc,docx,xls,xlsx,ppt,pdf,htm,html,txt,zip,rar,gz,bz2,mp3,wmv");
            extTable.Add("app", "apk,app,ipa");

            String dirName = context.Request["dir"];

            if (String.IsNullOrEmpty(dirName))
            {
                dirName = "image";
            }

            webSiteInfo = bll.GetWebsiteInfoModel();
            if (webSiteInfo == null)
            {
                return(ZentCloud.Common.JSONHelper.ObjectToJson(new
                {
                    errcode = 2,
                    errmsg = "找不到站点信息。"
                }));
            }
            currentUserInfo = bll.GetCurrentUserInfo();
            if (currentUserInfo != null)
            {
                userId = currentUserInfo.UserID;
            }
            else
            {
                userId = "other";
            }

            List <string>  fileUrlList   = new List <string>();
            List <dynamic> otherInfoList = new List <dynamic>();

            var postFileList = context.Request.Files;

            for (int i = 0; i < postFileList.Count; i++)
            {
                String fileName = postFileList[i].FileName;
                String fileExt  = Path.GetExtension(fileName).ToLower();

                if (String.IsNullOrEmpty(fileExt) || Array.IndexOf(((String)extTable[dirName]).Split(','), fileExt.Substring(1).ToLower()) == -1)
                {
                    return(ZentCloud.Common.JSONHelper.ObjectToJson(new
                    {
                        errcode = 1,
                        errmsg = "上传文件扩展名是不允许的扩展名。\n只允许" + ((String)extTable[dirName]) + "格式。"
                    }));
                }
                try
                {
                    int maxWidth  = 800;
                    int maxHeight = 0;
                    if (!string.IsNullOrWhiteSpace(context.Request["maxWidth"]))
                    {
                        maxWidth = Convert.ToInt32(context.Request["maxWidth"]);
                    }
                    if (!string.IsNullOrWhiteSpace(context.Request["maxHeight"]))
                    {
                        maxHeight = Convert.ToInt32(context.Request["maxHeight"]);
                    }
                    if (dirName == "image" && (maxWidth > 0 || maxHeight > 0))
                    {
                        ZentCloud.Common.ImgWatermarkHelper imgHelper = new ZentCloud.Common.ImgWatermarkHelper();
                        Image image = Image.FromStream(postFileList[i].InputStream);

                        if ((image.Width > maxWidth && maxWidth > 0) || (image.Height > maxHeight && maxHeight > 0))
                        {
                            double ratio     = imgHelper.GetRatio(image.Width, image.Height, maxWidth, maxHeight);
                            int    newWidth  = Convert.ToInt32(Math.Round(ratio * image.Width));
                            int    newHeight = Convert.ToInt32(Math.Round(ratio * image.Height));
                            image = imgHelper.PhotoSizeChange(image, newWidth, newHeight);
                        }
                        if (ZentCloud.Common.ConfigHelper.GetConfigInt("Oss_Enable") == 0)
                        {
                            //文件保存目录路径
                            String fname      = Guid.NewGuid().ToString("N").ToUpper() + fileExt;
                            String savePath   = "/FileUpload/" + dirName + "/" + bll.WebsiteOwner + "/" + userId + "/" + DateTime.Now.ToString("yyyyMMdd", DateTimeFormatInfo.InvariantInfo) + "/";
                            String serverPath = context.Server.MapPath(savePath);
                            if (!Directory.Exists(serverPath))
                            {
                                Directory.CreateDirectory(serverPath);
                            }
                            String serverFile = serverPath + fname;
                            String saveFile   = savePath + fname;
                            image.Save(serverFile, imgHelper.GetImageFormat(fileExt));

                            fileUrlList.Add(saveFile);

                            otherInfoList.Add(new
                            {
                                width      = image.Width,
                                height     = image.Height,
                                old_name   = postFileList[i].FileName,
                                filelength = postFileList[i].ContentLength
                            });
                        }
                        else
                        {
                            Stream stream = new MemoryStream();
                            image.Save(stream, imgHelper.GetImageFormat(fileExt));
                            stream.Position = 0;

                            string fileUrl = bllUploadOtherServer.upload(stream, fileExt); //= OssHelper.UploadFileFromStream(OssHelper.GetBucket(webSiteInfo.WebsiteOwner), OssHelper.GetBaseDir(webSiteInfo.WebsiteOwner), userId, "image", stream, fileExt);

                            fileUrlList.Add(fileUrl);

                            otherInfoList.Add(new
                            {
                                width      = image.Width,
                                height     = image.Height,
                                old_name   = postFileList[i].FileName,
                                filelength = postFileList[i].ContentLength
                            });
                        }
                    }
                    else
                    {
                        if (ZentCloud.Common.ConfigHelper.GetConfigInt("Oss_Enable") == 0)
                        {
                            //文件保存目录路径
                            String fname      = Guid.NewGuid().ToString("N").ToUpper() + fileExt;
                            String savePath   = "/FileUpload/" + dirName + "/" + bll.WebsiteOwner + "/" + userId + "/" + DateTime.Now.ToString("yyyyMMdd", DateTimeFormatInfo.InvariantInfo) + "/";
                            String serverPath = context.Server.MapPath(savePath);
                            if (!Directory.Exists(serverPath))
                            {
                                Directory.CreateDirectory(serverPath);
                            }
                            String serverFile = serverPath + fname;
                            String saveFile   = savePath + fname;
                            fileUrlList.Add(saveFile);
                            if (dirName == "image")
                            {
                                ZentCloud.Common.ImgWatermarkHelper imgHelper = new ZentCloud.Common.ImgWatermarkHelper();
                                Image image = Image.FromStream(postFileList[i].InputStream);
                                image.Save(serverFile, imgHelper.GetImageFormat(fileExt));
                                otherInfoList.Add(new
                                {
                                    width      = image.Width,
                                    height     = image.Height,
                                    old_name   = postFileList[i].FileName,
                                    filelength = postFileList[i].ContentLength
                                });
                            }
                            else
                            {
                                postFileList[i].SaveAs(serverFile);
                                otherInfoList.Add(new
                                {
                                    old_name    = postFileList[i].FileName,
                                    file_length = postFileList[i].ContentLength
                                });
                            }
                        }
                        else
                        {
                            String fileUrl = OssHelper.UploadFile(OssHelper.GetBucket(webSiteInfo.WebsiteOwner), OssHelper.GetBaseDir(webSiteInfo.WebsiteOwner), userId, dirName, postFileList[i]);
                            fileUrlList.Add(fileUrl);

                            if (dirName == "image")
                            {
                                Image image = Image.FromStream(postFileList[i].InputStream);
                                otherInfoList.Add(new
                                {
                                    width      = image.Width,
                                    height     = image.Height,
                                    old_name   = postFileList[i].FileName,
                                    filelength = postFileList[i].ContentLength
                                });
                            }
                            else
                            {
                                otherInfoList.Add(new
                                {
                                    old_name    = postFileList[i].FileName,
                                    file_length = postFileList[i].ContentLength
                                });
                            }
                        }
                    }
                }
                catch (Exception ex)
                {
                    if (ZentCloud.Common.ConfigHelper.GetConfigInt("Oss_Enable") == 0)
                    {
                        return(ZentCloud.Common.JSONHelper.ObjectToJson(new
                        {
                            errcode = 4,
                            errmsg = "上传出错:" + ex.Message
                        }));
                    }
                    else
                    {
                        return(ZentCloud.Common.JSONHelper.ObjectToJson(new
                        {
                            errcode = 4,
                            errmsg = "上传到Oss出错:" + ex.Message
                        }));
                    }
                }
            }
            //
            if (fileUrlList.Count > 0)
            {
                return(ZentCloud.Common.JSONHelper.ObjectToJson(new
                {
                    errcode = 0,
                    errmsg = "ok",
                    file_url_list = fileUrlList,
                    other_info_list = otherInfoList
                }));
            }
            else
            {
                return(ZentCloud.Common.JSONHelper.ObjectToJson(new
                {
                    errcode = 1,
                    errmsg = "fail"
                }));
            }
        }
Esempio n. 3
0
        ///// <summary>
        ///// 获取折线图数据
        ///// </summary>
        ///// <param name="context"></param>
        ///// <returns></returns>
        //private string GetLineChartData(HttpContext context)
        //{
        //    string chartItemStr = context.Request["ChartItem"];

        //    int weekChangeValue = Convert.ToInt32(context.Request["weekChangeValue"]);

        //    List<DateTime> arrDate = new List<DateTime>();

        //    DateTime monday = ZentCloud.Common.DateTimeHelper.CalculateFirstDateOfWeek(DateTime.Now);
        //    // DateTime monday = DateTime.Now;
        //    //测试需要暂时调数据为上周
        //    monday = monday.AddDays(weekChangeValue);

        //    arrDate.Add(monday);
        //    arrDate.Add(monday.AddDays(1));
        //    arrDate.Add(monday.AddDays(2));
        //    arrDate.Add(monday.AddDays(3));
        //    arrDate.Add(monday.AddDays(4));
        //    arrDate.Add(monday.AddDays(5));
        //    arrDate.Add(monday.AddDays(6));

        //    List<List<object>> result = new List<List<object>>();

        //    List<object> arrTitle = new List<object>() {
        //        "date"
        //        //,
        //        //"新增用户",
        //        //"新发文章",
        //        //"新发活动",
        //        //"分享数",
        //        //"报名人数"
        //        //,
        //        //"PC主页访问(IP)",
        //        //"PC主页访问(UV)",
        //        //"PC主页访问(PV)",
        //        //"手机主页访问(IP)",
        //        //"手机主页访问(UV)",
        //        //"手机主页访问(PV)",
        //        //"文章活动访问(IP)",
        //        //"文章活动访问(UV)",
        //        //"文章活动访问(PV)"
        //    };//图表显示列

        //    if (string.IsNullOrWhiteSpace(chartItemStr))
        //    {
        //        arrTitle.AddRange(new List<object>() {
        //            "新增用户",
        //            "新发文章",
        //            "新发活动"
        //        });
        //    }
        //    else
        //    {
        //        arrTitle.AddRange(
        //                chartItemStr.Split(new char[] { ',' }, StringSplitOptions.RemoveEmptyEntries)
        //            );
        //    }

        //    result.Add(arrTitle);//添加标题行
        //    Random r = new Random();
        //    foreach (var d in arrDate)
        //    {
        //        List<object> valueList = new List<object>();

        //        foreach (var t in arrTitle)
        //        {
        //            if (t.Equals("date"))
        //            {
        //                valueList.Add(string.Format("{0}月{1}号", d.Month.ToString(), d.Day.ToString()));//添加时间轴时间
        //            }
        //            else if (t.Equals("新增用户"))
        //            {
        //                valueList.Add(this.bll.GetNewUserCountByDate(d));
        //            }
        //            else if (t.Equals("新发文章"))
        //            {
        //                valueList.Add(this.bll.GetNewArticleCountByDate(d));
        //            }
        //            else if (t.Equals("新发活动"))
        //            {
        //                valueList.Add(this.bll.GetNewActivityCountByDate(d));
        //            }
        //            else if (t.Equals("分享数"))
        //            {
        //                valueList.Add(this.bll.GetShareCountByDate(d));
        //            }
        //            else if (t.Equals("报名人数"))
        //            {
        //                valueList.Add(this.bll.GetSignUpCountByDate(d));
        //            }
        //            else if (t.Equals("PC主页访问(IP)"))
        //            {
        //                valueList.Add(this.bll.GetPCIndexViewCountByDate(BLLJIMP.StatisticsType.IP, d));
        //            }
        //            else if (t.Equals("PC主页访问(UV)"))
        //            {
        //                valueList.Add(this.bll.GetPCIndexViewCountByDate(BLLJIMP.StatisticsType.UV, d));
        //            }
        //            else if (t.Equals("PC主页访问(PV)"))
        //            {
        //                valueList.Add(this.bll.GetPCIndexViewCountByDate(BLLJIMP.StatisticsType.PV, d));
        //            }
        //            else if (t.Equals("手机主页访问(IP)"))
        //            {
        //                valueList.Add(this.bll.GetMobileIndexViewCountByDate(BLLJIMP.StatisticsType.IP, d));
        //            }
        //            else if (t.Equals("手机主页访问(UV)"))
        //            {
        //                valueList.Add(this.bll.GetMobileIndexViewCountByDate(BLLJIMP.StatisticsType.UV, d));
        //            }
        //            else if (t.Equals("手机主页访问(PV)"))
        //            {
        //                valueList.Add(this.bll.GetMobileIndexViewCountByDate(BLLJIMP.StatisticsType.PV, d));
        //            }
        //            else if (t.Equals("文章活动访问(IP)"))
        //            {
        //                valueList.Add(this.bll.GetArticleViewCountByDate(BLLJIMP.StatisticsType.IP, d));
        //            }
        //            else if (t.Equals("文章活动访问(UV)"))
        //            {
        //                valueList.Add(this.bll.GetArticleViewCountByDate(BLLJIMP.StatisticsType.UV, d));
        //            }
        //            else if (t.Equals("文章活动访问(PV)"))
        //            {
        //                valueList.Add(this.bll.GetArticleViewCountByDate(BLLJIMP.StatisticsType.PV, d));
        //            }
        //            else
        //            {

        //            }


        //        }//添加查询值

        //        result.Add(valueList);//添加数据行
        //    }

        //    resp.Status = 1;
        //    resp.ExObj = result;

        //    return Common.JSONHelper.ObjectToJson(resp);
        //}
        /// <summary>
        /// 上传文件
        /// </summary>
        /// <param name="context"></param>
        /// <returns></returns>
        private string UploadSingelFile(HttpContext context)
        {
            if (context.Request.Files.Count == 0)
            {
                resp.Status = 0;
                resp.Msg    = "请选择文件!";
                return(Common.JSONHelper.ObjectToJson(resp));
            }

            HttpPostedFile file             = context.Request.Files[0];
            string         isNotNewFileName = context.Request["isnotnewfilename"];
            string         newFileExtension = Path.GetExtension(file.FileName).ToLower();
            string         newFileName      = "";

            if (!string.IsNullOrEmpty(isNotNewFileName))
            {
                newFileName = file.FileName;
            }
            else
            {
                newFileName = Guid.NewGuid().ToString("N").ToUpper() + newFileExtension;
            }

            if (!newFileName.EndsWith(".jpg") && !newFileName.EndsWith(".png") && !newFileName.EndsWith(".gif"))
            {
                resp.Status = 0;
                resp.Msg    = "请上传JPG、PNG和GIF格式图片!";
                return(Common.JSONHelper.ObjectToJson(resp));
            }

            if (currentUserInfo == null)
            {
                resp.Status = 0;
                resp.Msg    = "账户异常!";
                return(Common.JSONHelper.ObjectToJson(resp));
            }

            if (file != null)
            {
                try
                {
                    int maxWidth  = 800;
                    int maxHeight = 0;
                    if (!string.IsNullOrWhiteSpace(context.Request["maxWidth"]))
                    {
                        maxWidth = Convert.ToInt32(context.Request["maxWidth"]);
                    }
                    if (!string.IsNullOrWhiteSpace(context.Request["maxHeight"]))
                    {
                        maxHeight = Convert.ToInt32(context.Request["maxHeight"]);
                    }
                    if (maxWidth > 0 || maxHeight > 0)
                    {
                        Image image = Image.FromStream(file.InputStream);
                        ZentCloud.Common.ImgWatermarkHelper imgHelper = new ZentCloud.Common.ImgWatermarkHelper();
                        if ((image.Width > maxWidth && maxWidth > 0) || (image.Height > maxHeight && maxHeight > 0))
                        {
                            double ratio     = imgHelper.GetRatio(image.Width, image.Height, maxWidth, maxHeight);
                            int    newWidth  = Convert.ToInt32(Math.Round(ratio * image.Width));
                            int    newHeight = Convert.ToInt32(Math.Round(ratio * image.Height));
                            image = imgHelper.PhotoSizeChange(image, newWidth, newHeight);
                        }
                        if (ZentCloud.Common.ConfigHelper.GetConfigInt("Oss_Enable") == 0)
                        {
                            String savePath   = "/FileUpload/image/" + currentUserInfo.WebsiteOwner + "/" + currentUserInfo.UserID + "/" + DateTime.Now.ToString("yyyyMMdd", System.Globalization.DateTimeFormatInfo.InvariantInfo) + "/";
                            String serverPath = context.Server.MapPath(savePath);
                            if (!Directory.Exists(serverPath))
                            {
                                Directory.CreateDirectory(serverPath);
                            }
                            String serverFile = serverPath + newFileName;
                            String saveFile   = savePath + newFileName;
                            image.Save(serverFile, imgHelper.GetImageFormat(newFileExtension));
                            resp.ExStr = saveFile;
                        }
                        else
                        {
                            Stream stream = new MemoryStream();
                            image.Save(stream, imgHelper.GetImageFormat(newFileExtension));
                            stream.Position = 0;
                            resp.ExStr      = bllUploadOtherServer.upload(stream, newFileExtension);
                            //OssHelper.UploadFileFromStream(newFileName, OssHelper.GetBucket(currentUserInfo.WebsiteOwner), OssHelper.GetBaseDir(currentUserInfo.WebsiteOwner), currentUserInfo.UserID, "image", stream);
                        }
                        resp.ExObj = new
                        {
                            width  = image.Width,
                            height = image.Height
                        };
                    }
                    else
                    {
                        resp.ExStr = bllUploadOtherServer.uploadFromHttpPostedFile(file);
                        //if (string.IsNullOrEmpty(isNotNewFileName))
                        //{
                        //    resp.ExStr = OssHelper.UploadFile(OssHelper.GetBucket(currentUserInfo.WebsiteOwner), OssHelper.GetBaseDir(currentUserInfo.WebsiteOwner), currentUserInfo.UserID, "image", file);

                        //}
                        //else
                        //{
                        //    resp.ExStr = OssHelper.UploadFile(OssHelper.GetBucket(currentUserInfo.WebsiteOwner), OssHelper.GetBaseDir(currentUserInfo.WebsiteOwner), currentUserInfo.UserID, "image", file, false);

                        //}

                        Image image = Image.FromStream(file.InputStream);
                        resp.ExObj = new
                        {
                            width  = image.Width,
                            height = image.Height
                        };
                    }
                    resp.Status = 1;
                }
                catch (Exception ex)
                {
                    resp.Status = 0;
                    resp.Msg    = ex.Message;
                }
            }
            else
            {
                resp.Status = 0;
                resp.Msg    = "找不到文件!";
            }

            return(Common.JSONHelper.ObjectToJson(resp));
        }