Example #1
0
        public JsonResult UpImage(long?id) //id传过来,如需保存可以备用
        {
            int    success = 0;
            string msg     = "";
            string pathNew = "";

            try
            {
                var date  = Request;
                var files = Request.Form.Files;
                foreach (var formFile in files)
                {
                    if (formFile.Length > 0)
                    {
                        string fileExt     = formFile.FileName.Substring(formFile.FileName.LastIndexOf(".") + 1, (formFile.FileName.Length - formFile.FileName.LastIndexOf(".") - 1)); //扩展名
                        long   fileSize    = formFile.Length;                                                                                                                          //获得文件大小,以字节为单位
                        string md5         = GlobalMethod.GenerateMD5(formFile.OpenReadStream());
                        string newFileName = md5 + "." + fileExt;                                                                                                                      //MD5加密生成文件名保证文件不会重复上传
                        var    pathStart   = configuration["Location:ArticleImage"] + "/images/" + DateTime.Now.Year + "/" + DateTime.Now.Month + "/";
                        if (System.IO.Directory.Exists(pathStart) == false)                                                                                                            //如果不存在新建
                        {
                            System.IO.Directory.CreateDirectory(pathStart);
                        }
                        var filePath = pathStart + newFileName;
                        pathNew = filePath.Replace(configuration["Location:ArticleImage"], "");
                        using (var stream = new FileStream(filePath, FileMode.Create))
                        {
                            formFile.CopyTo(stream);
                            success = 1;
                        }
                    }
                }
            }
            catch (Exception ex)
            {
                success = 0;
                msg     = ex.ToString();
            }
            var obj = new { success = success, url = pathNew, message = msg };

            return(Json(obj));
        }
Example #2
0
        private string saveImage(IFormFileCollection files, string ProjectId, string AriticleId, out string message)
        {
            string pathNew = "";

            message = "";
            foreach (var formFile in files)
            {
                if (formFile.Length > 0)
                {
                    string fileExt     = formFile.FileName.Substring(formFile.FileName.LastIndexOf(".") + 1, (formFile.FileName.Length - formFile.FileName.LastIndexOf(".") - 1)); //扩展名
                    long   fileSize    = formFile.Length;                                                                                                                          //获得文件大小,以字节为单位
                    string md5         = GlobalMethod.GenerateMD5(formFile.OpenReadStream());
                    string newFileName = md5 + "." + fileExt;                                                                                                                      //MD5加密生成文件名保证文件不会重复上传
                    var    pathStart   = configuration["Location:ArticleImage"] + "/images/Project" + ProjectId + "/Article" + AriticleId + "/";
                    if (!Directory.Exists(pathStart))                                                                                                                              //如果不存在新建
                    {
                        message += "新建pathStart:" + pathStart;
                        Directory.CreateDirectory(pathStart);
                        message += "未跳出";
                    }
                    else
                    {
                        message += "未新建pathStart:" + pathStart;
                    }
                    var filePath = pathStart + newFileName;
                    pathNew = filePath.Replace(configuration["Location:ArticleImage"], "");
                    using (var stream = new FileStream(filePath, FileMode.Create))
                    {
                        message += "进入copy";
                        formFile.CopyTo(stream);
                    }
                    message += "未进入copy";
                }
            }
            message += "Over";
            return(pathNew);
        }