示例#1
0
        private void UploadFile()
        {
            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 _isThumbnail = true;  //默认不生成缩略图

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

            if (status == "1")
            {
                string filePath        = dict["path"].ToString();
                string fileFullPath    = dict["fullpath"].ToString();
                string thumbPath       = dict["thumb"].ToString();
                string fileSize        = dict["size"].ToString();
                string fileExt         = dict["ext"].ToString();
                string _mediaType      = HttpContext.Request.Query["MediaType"];
                string _isForeverMedia = HttpContext.Request.Query["IsForeverMedia"];
                if (_mediaType == "image" && _isForeverMedia == "1")
                {
                    var uploadForeverMediaResult = UploadForeverMedia(fileFullPath);
                    if (uploadForeverMediaResult.ErrorCodeValue == 0)
                    {
                        SaveWxImage(fileName, filePath, thumbPath, fileSize, fileExt, uploadForeverMediaResult.media_id, uploadForeverMediaResult.url);
                        showSuccess(fileName, filePath, fileFullPath, thumbPath, fileSize, fileExt);
                    }
                }
            }
            else
            {
                showError(msg);
            }
        }
示例#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.WxUpload().FileSaveAs(byteData, fileName, _isThumbnail, _iswater);
            Dictionary <string, object> dict = JsonHelper.DataRowFromJSON(remsg);
            string status = dict["status"].ToString();
            string msg    = dict["msg"].ToString();

            if (status == "1")
            {
                string filePath        = dict["path"].ToString();
                string fileFullPath    = dict["fullpath"].ToString();
                string thumbPath       = dict["thumb"].ToString();
                string fileSize        = dict["size"].ToString();
                string fileExt         = dict["ext"].ToString();
                var    uploadImgResult = UploadImg(fileFullPath);
                if (uploadImgResult.ErrorCodeValue == 0)
                {
                    editorUploadSuccess(uploadImgResult.url, fileName, fileName);
                }
            }
            else
            {
                showError(msg);
            }
        }