Exemple #1
0
        public void UploadImageData(HttpContext context)
        {
            var            respObj    = new UploadFileResp();
            HttpPostedFile postedFile = context.Request.Files["file"];

            if (postedFile == null)
            {
                postedFile = context.Request.Files[0];
            }
            if (postedFile == null || postedFile.ContentLength == 0)
            {
                respObj.success = false;
                respObj.msg     = "文件不能为空";
                respObj.file    = new FileData();
                context.Response.Write(respObj.ToJSON());
                return;
            }
            string folderPath = "Framework/Upload/Image/" + Utils.GetTodayString() + "/";
            string savepath   = HttpContext.Current.Server.MapPath("~/" + folderPath);//http://bs.dev.chainclouds.com/
            var    extension  = Path.GetExtension(postedFile.FileName).ToLower();

            if (!Directory.Exists(savepath))
            {
                Directory.CreateDirectory(savepath);
            }

            var host = ConfigurationManager.AppSettings["host"];

            if (!host.EndsWith("/"))
            {
                host += "/";
            }

            var fileName     = Guid.NewGuid().ToString().Replace("-", string.Empty).ToUpper(); //文件名用guid来取
            var fileFullName = fileName + extension;
            var fileLocation = string.Format("{0}/{1}", savepath, fileFullName);               //把文件夹路径和图片名称放在一起

            postedFile.SaveAs(fileLocation);                                                   //保存原图

            string targetDir = "image/" + Utils.GetTodayString();

            System.Drawing.Image originalImage = System.Drawing.Image.FromFile(fileLocation);
            //生成缩略图
            respObj.thumbs = new List <FileData>();
            if (true)
            {
                int thumbWidth    = 120;
                var thumbFullName = fileName + "_" + thumbWidth.ToString() + extension;
                var thumbLocation = string.Format("{0}/{1}_{2}{3}", savepath, fileName, thumbWidth, extension);
                if (MakeThumbnail(originalImage, thumbLocation, thumbWidth, thumbWidth, "W"))
                {
                    var    thumbImage = new FileInfo(thumbLocation);       //根据地址获取生成的缩略图
                    string thumbUrl   = host + folderPath + thumbFullName;
                    thumbUrl = getFtpUrl(thumbImage, thumbUrl, targetDir); //上传到ftp
                    respObj.thumbs.Add(new FileData()
                    {
                        url       = thumbUrl,
                        name      = thumbFullName,
                        extension = extension,
                        size      = thumbImage.Length,
                        type      = thumbWidth.ToString()
                    });
                }
            }
            if (true)
            {
                int thumbWidth    = 240;
                var thumbFullName = fileName + "_" + thumbWidth.ToString() + extension;
                var thumbLocation = string.Format("{0}/{1}_{2}{3}", savepath, fileName, thumbWidth, extension);
                if (MakeThumbnail(originalImage, thumbLocation, thumbWidth, thumbWidth, "W"))
                {
                    var    thumbImage = new FileInfo(thumbLocation);
                    string thumbUrl   = host + folderPath + thumbFullName;
                    thumbUrl = getFtpUrl(thumbImage, thumbUrl, targetDir);//上传到ftp
                    respObj.thumbs.Add(new FileData()
                    {
                        url       = thumbUrl,
                        name      = thumbFullName,
                        extension = extension,
                        size      = thumbImage.Length,
                        type      = thumbWidth.ToString()
                    });
                }
            }
            if (true)
            {
                int thumbWidth    = 480;
                var thumbFullName = fileName + "_" + thumbWidth.ToString() + extension;
                var thumbLocation = string.Format("{0}/{1}_{2}{3}", savepath, fileName, thumbWidth, extension);
                if (MakeThumbnail(originalImage, thumbLocation, thumbWidth, thumbWidth, "W"))
                {
                    var    thumbImage = new FileInfo(thumbLocation);
                    string thumbUrl   = host + folderPath + thumbFullName;
                    thumbUrl = getFtpUrl(thumbImage, thumbUrl, targetDir);//上传到ftp
                    respObj.thumbs.Add(new FileData()
                    {
                        url       = thumbUrl,
                        name      = thumbFullName,
                        extension = extension,
                        size      = thumbImage.Length,
                        type      = thumbWidth.ToString()
                    });
                }
            }
            if (true)//商品详情里用的图片
            {
                int thumbWidth    = 640;
                var thumbFullName = fileName + "_" + thumbWidth.ToString() + extension;
                var thumbLocation = string.Format("{0}/{1}_{2}{3}", savepath, fileName, thumbWidth, extension);
                if (MakeThumbnail(originalImage, thumbLocation, thumbWidth, thumbWidth, "W"))
                {
                    var    thumbImage = new FileInfo(thumbLocation);
                    string thumbUrl   = host + folderPath + thumbFullName;
                    thumbUrl = getFtpUrl(thumbImage, thumbUrl, targetDir);//上传到ftp
                    respObj.thumbs.Add(new FileData()
                    {
                        url       = thumbUrl,
                        name      = thumbFullName,
                        extension = extension,
                        size      = thumbImage.Length,
                        type      = thumbWidth.ToString()
                    });
                }
            }
            if (true)
            {
                int thumbWidth    = 960;
                var thumbFullName = fileName + "_" + thumbWidth.ToString() + extension;
                var thumbLocation = string.Format("{0}/{1}_{2}{3}", savepath, fileName, thumbWidth, extension);
                if (MakeThumbnail(originalImage, thumbLocation, thumbWidth, thumbWidth, "W"))
                {
                    var    thumbImage = new FileInfo(thumbLocation);
                    string thumbUrl   = host + folderPath + thumbFullName;
                    thumbUrl = getFtpUrl(thumbImage, thumbUrl, targetDir);//上传到ftp
                    respObj.thumbs.Add(new FileData()
                    {
                        url       = thumbUrl,
                        name      = thumbFullName,
                        extension = extension,
                        size      = thumbImage.Length,
                        type      = thumbWidth.ToString()
                    });
                }
            }

            respObj.o       = true;
            respObj.success = true;
            respObj.msg     = "";
            respObj.file    = new FileData();
            //传ftp
            #region
            string   tempFileUrl = host + folderPath + fileFullName;
            FileInfo fileInfo    = new FileInfo(fileLocation);
            tempFileUrl = getFtpUrl(fileInfo, tempFileUrl, targetDir);//上传到ftp
            #endregion
            respObj.file.url       = tempFileUrl;
            respObj.file.name      = fileFullName;
            respObj.file.extension = extension;
            respObj.file.size      = postedFile.ContentLength;
            if (originalImage != null)
            {
                originalImage.Dispose();
            }


            context.Response.Write(respObj.ToJSON());
            context.Response.End();
        }
Exemple #2
0
        protected void ProcessAction(HttpContext context)
        {
            //context.Response.ContentType = "multipart/form-data";
            //context.Response.Expires = -1;
            try
            {
                context.Response.Clear();
                //string content = "";
                switch (context.Request.QueryString["method"])
                {
                case "image":
                    UploadImageData(context);
                    break;

                case "file":
                default:
                    var            respObj    = new UploadFileResp();
                    HttpPostedFile postedFile = context.Request.Files["file"];
                    if (postedFile == null)
                    {
                        postedFile = context.Request.Files[0];
                    }
                    if (postedFile == null || postedFile.ContentLength == 0)
                    {
                        respObj.success = false;
                        respObj.msg     = "文件不能为空";
                        respObj.file    = new FileData();
                        context.Response.Write(respObj.ToJSON());
                        return;
                    }
                    string folderPath = "Framework/Upload/File/" + Utils.GetTodayString() + "/";
                    string savepath   = HttpContext.Current.Server.MapPath("~/" + folderPath);
                    var    extension  = Path.GetExtension(postedFile.FileName).ToLower();
                    if (!Directory.Exists(savepath))
                    {
                        Directory.CreateDirectory(savepath);
                    }

                    var host = ConfigurationManager.AppSettings["host"];
                    if (!host.EndsWith("/"))
                    {
                        host += "/";
                    }

                    var fileName     = Guid.NewGuid().ToString().Replace("-", string.Empty).ToUpper() + extension;
                    var fileLocation = string.Format("{0}/{1}", savepath, fileName);
                    postedFile.SaveAs(fileLocation);

                    respObj.o       = true;
                    respObj.success = true;
                    respObj.msg     = "";
                    respObj.file    = new FileData();
                    #region
                    string   targetDir   = "file/" + Utils.GetTodayString();
                    string   tempFileUrl = host + folderPath + fileName;
                    FileInfo fileInfo    = new FileInfo(fileLocation);
                    tempFileUrl = getFtpUrl(fileInfo, tempFileUrl, targetDir);    //上传到ftp
                    #endregion
                    respObj.file.url       = tempFileUrl;
                    respObj.file.localurl  = folderPath + fileName;
                    respObj.file.name      = fileName;
                    respObj.file.extension = extension;
                    respObj.file.size      = postedFile.ContentLength;

                    context.Response.Write(respObj.ToJSON());
                    context.Response.End();
                    break;
                }
            }
            catch (System.Threading.ThreadAbortException exf)
            {
            }
            catch (Exception ex)
            {
                context.Response.Clear();
                var respObj = new UploadFileResp();
                respObj.success = false;
                respObj.msg     = ex.Message;
                context.Response.Write(respObj.ToJSON());
                context.Response.End();
            }
        }