Ejemplo n.º 1
0
        private void UploadImage()
        {
            //上传配置
            //String pathbase = UploadPath +string.Format("/{0}/", CurrentUser.ID);                                      //保存路径
            String pathbase = UploadPath;                                      //保存路径
            string[] filetype = { ".gif", ".png", ".jpg", ".jpeg", ".bmp" };          //文件允许格式
            int size = 1024;                                                          //文件大小限制,单位KB

            //文件上传状态,初始默认成功,可选参数{"SUCCESS","ERROR","SIZE","TYPE"}

            String filename = String.Empty;
            String currentType = String.Empty;
            String uploadpath = String.Empty;

            uploadpath = Server.MapPath(pathbase);

            try {
                HttpPostedFile uploadFile = Request.Files[0];
                title = uploadFile.FileName;

                //目录验证
                if (!Directory.Exists(uploadpath)) {
                    Directory.CreateDirectory(uploadpath);
                }

                if (!Directory.Exists(uploadpath + "/120x120/")) {
                    Directory.CreateDirectory(uploadpath + "/120x120/");
                }

                //格式验证
                string[] temp = title.Split('.');
                currentType = "." + temp[temp.Length - 1];
                if (Array.IndexOf(filetype, currentType) == -1) {
                    state = "TYPE";
                }

                //大小验证
                if (uploadFile.ContentLength / 1024 > size) {
                    state = "SIZE";
                }

                //保存图片
                if (state == "SUCCESS") {
                    filename = DateTime.Now.ToString("yyyyMMddHHmmssfff") + '-' + rnd.Next() + currentType;
                    uploadFile.SaveAs(uploadpath + filename);

                    Xy.Tools.ImageHandler _IH = new Xy.Tools.ImageHandler(title, uploadFile.InputStream);
                    _IH.GetImage(120, 120, Xy.Tools.ImageHandler.ResizeType.stretch, Xy.Tools.ImageHandler.ResizeType.ignore);
                    _IH.Save(uploadpath + "/120x120/" + filename);
                    url = pathbase + filename;
                }
            } catch (Exception ex) {
                state = "ERROR";
                error = ex.Message;
            }

            //获取图片描述
            if (Request.Form["pictitle"] != null) {
                if (!String.IsNullOrEmpty(Request.Form["pictitle"])) {
                    title = Request.Form["pictitle"];
                }
            }

            url = url.Replace("../", "");
            returnString = "{'url':'" + url + "','title':'" + title + "','state':'" + state + "'" + (String.IsNullOrEmpty(error) ? String.Empty : (",'error':'" + error + "'").Replace(Environment.NewLine, String.Empty)) + "}";
        }
Ejemplo n.º 2
0
        public override void ValidateUrl()
        {
            Xy.Tools.ImageHandler _IH = null;
            string UploadPath = "/Upload/";
            string currentType = string.Empty;

            if (!string.IsNullOrEmpty(Request.QueryString["path"])) {
                UploadPath += Request.QueryString["path"] + "/";
            }

            try {
                // Get the data
                HttpPostedFile upload_file = Request.Files["Filedata"];
                _IH = new Xy.Tools.ImageHandler(upload_file.FileName, upload_file.InputStream);

                string fileName = DateTime.Now.ToString("yyyyMMddHHmmssfff") + '-' + rnd.Next() + _IH.Type;

                string relativepath, saveFolder;

                switch (_IH.Type) {
                    case ".jpg":
                    case ".jpeg":
                    case ".bmp":
                    case ".png":
                    case ".gif":
                        relativepath = UploadPath + "/original/";
                        saveFolder = Server.MapPath(relativepath);
                        if (!System.IO.Directory.Exists(saveFolder)) {
                            System.IO.Directory.CreateDirectory(saveFolder);
                        }
                        _IH.Save(saveFolder + fileName);

                        int target_width, target_height;
                        Xy.Tools.ImageHandler.ResizeType target_width_resize, target_height_resize;
                        if (!string.IsNullOrEmpty(Request.QueryString["Thumbnail"])) {
                            string[] sizes = Request.QueryString["Thumbnail"].Split(',');
                            for (int i = 0; i < sizes.Length; i++) {
                                target_width = target_height = 0; target_width_resize = target_height_resize = 0;
                                string[] size = sizes[i].Split('|');
                                string[] types;
                                for (int j = 0; j < 4; j++) {
                                    switch (j) {
                                        case 0:
                                            target_width = Convert.ToInt32(size[j]);
                                            break;
                                        case 1:
                                            target_height = Convert.ToInt32(size[j]);
                                            break;
                                        case 2:
                                            types = size[j].Split('&');
                                            foreach (string type in types) {
                                                target_width_resize = target_width_resize | (Xy.Tools.ImageHandler.ResizeType)Convert.ToInt32(type);
                                            }
                                            break;
                                        case 3:
                                            types = size[j].Split('&');
                                            foreach (string type in types) {
                                                target_height_resize = target_height_resize | (Xy.Tools.ImageHandler.ResizeType)Convert.ToInt32(type);
                                            }
                                            break;
                                    }
                                }
                                _IH.GetImage(target_width, target_height, target_width_resize, target_height_resize);

                                relativepath = UploadPath + string.Format("/{0}x{1}/", target_width, target_height);
                                saveFolder = Server.MapPath(relativepath);
                                if (!System.IO.Directory.Exists(saveFolder)) {
                                    System.IO.Directory.CreateDirectory(saveFolder);
                                }
                                _IH.Save(saveFolder + fileName);
                                //returnString += "?" + relativepath + fileName;
                            }
                        }
                        break;
                    default:
                        relativepath = UploadPath + "/other/";
                        saveFolder = Server.MapPath(relativepath);
                        if (!System.IO.Directory.Exists(saveFolder)) {
                            System.IO.Directory.CreateDirectory(saveFolder);
                        }
                        upload_file.SaveAs(saveFolder + fileName);
                        break;
                }
                returnString = fileName;
                Response.StatusCode = 200;

            } catch (Exception ex) {
                if (string.IsNullOrEmpty(returnString)) {
                    //Response.StatusCode = 500;
                    returnString = "Error: " + ex.Message;
                }
            } finally {
                // Clean up
                if (_IH != null) _IH.Dispose();
                Response.Write(returnString);
            }
        }