Example #1
0
        private void UploadFile()
        {
            //检查是否允许匿名上传

            /*if (sysConfig.fileanonymous == 0 && !new ManagePage().IsAdminLogin() && !new BasePage().IsUserLogin())
             * {
             *  context.Response.Write("{\"status\": 0, \"msg\": \"禁止匿名非法上传!\"}");
             *  return;
             * }*/

            //string _delfile = HttpContext.Request.Query["DelFilePath"]; //要删除的文件
            //string fileName = HttpContext.Request.Query["name"]; //文件名
            var    upfile   = HttpContext.Request.Form.Files["upfile"];
            string fileName = upfile.FileName;
            string fileType = upfile.ContentType;

            byte[] byteData; //获取文件流
            using (var stream = upfile.OpenReadStream())
            {
                byteData = new byte[stream.Length];
                stream.Read(byteData, 0, (int)stream.Length);
            }

            bool _isWater = false; //默认不打水印
            bool _isThumb = false; //默认不生成缩略图

            if (HttpContext.Request.Query["IsWater"] == "1")
            {
                _isWater = true;
            }
            if (HttpContext.Request.Query["IsThumb"] == "1")
            {
                _isThumb = true;
            }
            if (byteData.Length == 0)
            {
                //context.Response.Write("{\"status\": 0, \"msg\": \"请选择要上传文件!\"}");
                return;
            }
            string webRootPath = _hostingEnvironment.WebRootPath;
            string remsg       = new NFineCore.Support.Upload().FileSaveAs(byteData, fileName, _isThumb, _isWater);
            Dictionary <string, object> dic = JsonHelper.DataRowFromJSON(remsg);
            string status = dic["status"].ToString();
            string msg    = dic["msg"].ToString();

            if (status == "1")
            {
                string filePath  = dic["path"].ToString(); //取得上传后的路径
                string thumbPath = dic["thumb"].ToString();
                string fileSize  = dic["size"].ToString();
                string fileExt   = dic["ext"].ToString();
                SaveAttach(fileName, fileType, filePath, thumbPath, fileSize, fileExt);
                showSuccess(fileName, filePath, thumbPath, fileSize, fileExt);
            }
            else
            {
                showError(msg);
            }
        }
Example #2
0
        public void EditorUploadImage()
        {
            string webRootPath     = _hostingEnvironment.WebRootPath;
            string contentRootPath = _hostingEnvironment.ContentRootPath;
            bool   _iswater        = false; //默认不打水印
            bool   _isThumbnail    = true;  //默认生成缩略图

            if (HttpContext.Request.Query["IsWater"] == "1")
            {
                _iswater = true;
            }
            var    upfile   = HttpContext.Request.Form.Files["upfile"];
            string fileName = upfile.FileName;

            byte[] byteData; //获取文件流
            using (var stream = upfile.OpenReadStream())
            {
                byteData = new byte[stream.Length];
                stream.Read(byteData, 0, (int)stream.Length);
            }
            //开始上传
            string remsg = new NFineCore.Support.Upload().FileSaveAs(byteData, fileName, _isThumbnail, _iswater);
            Dictionary <string, object> dic = JsonHelper.DataRowFromJSON(remsg);
            string status = dic["status"].ToString();
            string msg    = dic["msg"].ToString();

            if (status == "1")
            {
                //string appId = WxOperatorProvider.Provider.GetCurrent().AppId;
                //AccessTokenResult accessTokenResult = AccessTokenContainer.GetAccessTokenResult(appId);

                //string filePath = dic["path"].ToString(); //取得上传后的路径
                //string thumbPath = dic["thumb"].ToString();
                //string fileSize = dic["size"].ToString();
                //string fileExt = dic["ext"].ToString();
                //string title = fileName;
                //string original = fileName;

                //UploadImgResult uploadImgResult = MediaApi.UploadImg(accessTokenResult.access_token, webRootPath+filePath);
                //string url = uploadImgResult.url;
                //editorUploadSuccess(url, title, original);
            }
            else
            {
                showError(msg);
            }
        }