关闭

MVC下xheditor上传图片Upload的action图片保存

1181人阅读 评论(0) 收藏 举报
/// <summary>
        /// 上传图片
        /// </summary>
        /// <param name="fileData"></param>
        /// <returns></returns>
        [HttpPost]
        public ActionResult Upload(HttpPostedFileBase fileData)
        {
            string result = "0";
            string folder = "/UploadFiles/NoticeFiles";
            string errMsg = "";

            if (fileData != null)
            {
                try
                {
                    string fileName = fileData.FileName;
                    if (fileName.LastIndexOf("\\") > 0)
                    {
                        fileName = fileName.Substring(fileName.LastIndexOf("\\") + 1);
                    }
                    string currentTime = DateTime.Now.ToString("yyyyMMddhhmmss");
                    Random r = new Random(DateTime.Now.Millisecond);
                    int rndKey = r.Next(100000, 999999);

                    result = Path.GetFileName(fileName);//获得文件名,返回进行显示
                    string saveFileName = string.Format("{0}_{1}", currentTime + rndKey.ToString(), fileName);

                    string saveFolder = Request.MapPath(string.Format("~{0}", folder));
                    if (!Directory.Exists(saveFolder))
                        Directory.CreateDirectory(saveFolder);

                    fileData.SaveAs(string.Format("{0}\\{1}", saveFolder, saveFileName));

                    result = string.Format("{0}/{1}", folder, saveFileName);

                }
                catch (Exception ex) { errMsg = ex.Message; }
            }

            var data = new { err = errMsg, msg = result };

            return this.Content(new JavaScriptSerializer().Serialize(data));//这里不能用Json方法

        }
0
0

查看评论
* 以上用户言论只代表其个人观点,不代表CSDN网站的观点或立场
    个人资料
    • 访问:144798次
    • 积分:1722
    • 等级:
    • 排名:千里之外
    • 原创:27篇
    • 转载:57篇
    • 译文:0篇
    • 评论:7条
    文章分类
    最新评论