Ejemplo n.º 1
0
        /// <summary>
        /// 上传
        /// </summary>
        /// <param name="folder"></param>
        /// <param name="file"></param>
        /// <param name="watermark"></param>
        /// <returns></returns>
        public Entity.AttachmentInfo save(string folder, HttpPostedFileBase file, bool watermark)
        {
            this.watermark = watermark;
            /// 文件文件设置
            Entity.AttachmentInfo attachInfo = new Entity.AttachmentInfo();
            string fileExt  = Path.GetExtension(file.FileName).ToLower();//扩展名
            string turnPath = string.Empty;

            if (config.AttachExtension.IndexOf(fileExt) == -1)
            {
                throw new Exception("文件类型错误!不允许的文件类型" + fileExt);
            }
            else if (file.ContentLength < 10 || file.ContentLength > config.AttachMaxSize)
            {
                throw new Exception("文件选择有误,请上传大于1k,小于" + config.AttachMaxSize / 1024 + "K 的文件!");
            }
            else
            {
                attachInfo.Size = file.ContentLength / 1024;
                if (attachInfo.Size < 0)
                {
                    attachInfo.Size = 1;
                }
                attachInfo.Title = Common.Utils.trimEnd(file.FileName, fileExt);

                string filePath = this.getFilePath(folder);

                string fileName = this.getFileName(fileExt);//文件名



                string fileExtension = ".jpe|.jpeg|.jpg|.png|.gif|.bmp";
                if (fileExtension.IndexOf(fileExt) > -1)//图片类型
                {
                    Image upImg = Image.FromStream(file.InputStream);

                    attachInfo.Width  = upImg.Width;
                    attachInfo.Height = upImg.Height;

                    saveImage(upImg, fileName, filePath);
                    upImg.Dispose();//销毁对象
                }
                else//保存文件
                {
                    string serverPath = Fetch.getMapPath(filePath);
                    if (!Directory.Exists(serverPath))//创建目录
                    {
                        Directory.CreateDirectory(serverPath);
                    }

                    file.SaveAs(serverPath + fileName);
                }

                attachInfo.Path = filePath + fileName;
            }
            return(attachInfo);
        }
Ejemplo n.º 2
0
        public JsonResult upload(string folder, int watermark)
        {
            var files = this.Request.Files;
            var path  = "";

            Entity.AttachmentInfo attachmentInfo = null;
            if (files.Count > 0)
            {
                try
                {
                    Common.Upload uploadFile = new Common.Upload();
                    attachmentInfo = uploadFile.save(folder, files.Get(0), watermark == 1);
                    path           = attachmentInfo.Path;
                }
                catch (Exception ex)
                {
                    return(getResult(bitcms.Entity.Error.错误, ex.ToString()));
                }
            }
            return(getResult(bitcms.Entity.Error.请求成功, "上传成功", path));
        }