Ejemplo n.º 1
0
        public ActionResult OnPostUploadFile(string title, IFormFile fileUpload)
        {
            IFormFile file = fileUpload;//Request.Form.Files[0];

            if ((file == null || file.Length == 0) || (title == null || title.Trim().Length == 0) || file.Length > 4194304)
            {
                return(new JsonResult(State.INVAILD_DATA));
            }

            MemoryStream memoryStream = new MemoryStream();

            file.CopyTo(memoryStream);
            string fileName   = Path.GetFileNameWithoutExtension(file.FileName);
            string extention  = Path.GetExtension(file.FileName);
            string localPath  = Path.Combine(Environment.WebRootPath, "files", title, fileName + extention);
            string serverPath = Path.Combine(Path.DirectorySeparatorChar.ToString(), "files", title, fileName + extention);

            Directory.CreateDirectory(Path.Combine(Environment.WebRootPath, "files", title));
            string[] keys = { "entry_title", "operator_type", "key_title", "value", "class" };

            MySqlIntegration sqlInteg = new MySqlIntegration(Configuration.GetConnectionString("MySqlConnection"));

            sqlInteg.MySqlInsert("repo_key_values", keys, title, Operator.File, "file_title", fileName + extention, "string");
            sqlInteg.MySqlInsert("repo_key_values", keys, title, Operator.File, "file_web_path", serverPath.Replace("\\", "\\\\"), "string");

            keys = new string[] { "title_unique", "title_display", "operator_type", "owner" };
            sqlInteg.MySqlInsert("repo_entries", keys, title, title, Operator.File, "anonymous");

            //Future Feature
            //sqlInteg.MySqlInsert("repo_key_values", keys, title, Operator.File, "description", localPath, "string");
            System.Threading.Thread.Sleep(2500);
            FileStream fileStream = null;

            try
            {
                fileStream = new FileStream(localPath, FileMode.Create);
                file.CopyTo(fileStream);
            }
            catch (IOException ex)
            {
                Debug.WriteLine(ex.Message);
                return(new JsonResult(State.IO_ERROR));
            }
            finally
            {
                if (fileStream != null)
                {
                    fileStream.Close();
                }
            }

            return(new JsonResult(JsonConvert.SerializeObject(State.SUCCESS)));
        }
Ejemplo n.º 2
0
        public ActionResult OnPostSave()
        {
            MemoryStream memoryStream = new MemoryStream();

            Request.Body.CopyTo(memoryStream);
            memoryStream.Position = 0;
            using (StreamReader reader = new StreamReader(memoryStream))
            {
                string result = reader.ReadToEnd();
                if (result.Length > 0 || !result.Trim().Equals(""))
                {
                    PageEntry = JsonConvert.DeserializeObject <Entry>(result);
                }
                else
                {
                    return(new JsonResult(JsonConvert.SerializeObject(0)));
                }
            }
            //变量验证
            if (BasicClasses.Find(basicClassObj => basicClassObj.ID == PageEntry.BasicClassID) == null)
            {
                PageEntry.BasicClassID = 0;
            }
            else
            {
                if (IsBasicEntry(PageEntry))
                {
                    PageEntry.BasicClassID = BasicClasses.Find(basicClassObj => basicClassObj.UniqueTitle == PageEntry.UniqueTitle).ID;
                }
            }
            PageEntry.PageContent = PageEntry.PageContent.Replace("\\", "\\\\");
            PageEntry.PageContent = PageEntry.PageContent.Replace("\'", "\\\'");
            string[]         keySelect = { "title_unique" };
            MySqlIntegration postInteg = new MySqlIntegration(Configuration.GetConnectionString("MySqlConnection"));

            postInteg.MySqlSelect("entries", keySelect, "title_unique=" + MySqlIntegration.QuoteStr(PageEntry.UniqueTitle));

            string[] keysSave = { "title_unique", "basic_class_ID", "title_display", "content" };
            int      lineAffected;

            if (postInteg.IntegratedResult.Count > 0)
            {
                lineAffected = postInteg.MySqlUpdate("entries", keysSave, "title_unique=" + MySqlIntegration.QuoteStr(PageEntry.UniqueTitle), PageEntry.UniqueTitle, PageEntry.BasicClassID, PageEntry.DisplayTitle, PageEntry.PageContent);
            }
            else
            {
                lineAffected = postInteg.MySqlInsert("entries", keysSave, PageEntry.UniqueTitle, PageEntry.BasicClassID, PageEntry.DisplayTitle, PageEntry.PageContent);
            }
            return(new JsonResult(JsonConvert.SerializeObject(lineAffected)));
        }
Ejemplo n.º 3
0
        public JsonResult OnPostSaveImage(string UniqueTitle)
        {
            if (ImageUpload == null || ImageUpload.Length == 0)
            {
                return(new JsonResult(""));
            }

            //current version of the image uploaded
            string version = DateTime.Now.ToString("yyyy-MM-dd HH-mm-ss");
            //The name of image
            string fileName = Path.GetFileName(ImageUpload.FileName);
            //The path of the image on the server
            string localPath = Path.Combine(Environment.WebRootPath, "uploads", UniqueTitle, fileName, version + Path.GetExtension(fileName));
            //The path of the image from wwwroot
            string serverPath = Path.Combine(Path.DirectorySeparatorChar.ToString(), "uploads", UniqueTitle, fileName, version + Path.GetExtension(fileName));

            Directory.CreateDirectory(Path.Combine(Environment.WebRootPath, "uploads", UniqueTitle, fileName));

            string[] keys = { "title_unique", "image_name", "version" };

            MySqlIntegration sqlInteg = new MySqlIntegration(Configuration.GetConnectionString("MySqlConnection"));

            sqlInteg.MySqlInsert("images", keys, UniqueTitle, fileName, version);
            FileStream fileStream = null;

            try
            {
                fileStream = new FileStream(localPath, FileMode.Create);
                ImageUpload.CopyTo(fileStream);
            }
            catch (IOException ex)
            {
                Debug.WriteLine(ex.Message);
                return(new JsonResult(""));
            }
            finally
            {
                if (fileStream != null)
                {
                    fileStream.Close();
                }
            }
            return(new JsonResult(JsonConvert.SerializeObject(serverPath)));
        }