Beispiel #1
0
        /// <summary>
        /// 上传文件
        /// </summary>
        /// <param name="file">文件</param>
        /// <returns></returns>
        public async Task <JsonResult> Upload(IFormFile file)
        {
            var fileSplit = file.FileName.Split('.');

            if (fileSplit.Length <= 1)
            {
                return(Json(new { success = false, message = "无法识别的扩展名" }));
            }
            var fileExtenions = "." + fileSplit[fileSplit.Length - 1];

            if (fileExtenions.ToLower() != ".jpg" && fileExtenions.ToLower() != ".png")
            {
                return(Json(new { success = false, message = "请上传 .jpg 或者 .png格式的图片" }));
            }
            var fileName = UploadFile.DateTimeToUnixTimestamp(DateTime.Now) + fileExtenions;
            var filePath = await new UploadFile(env).UploadImage(file, fileName);

            return(Json(new { success = true, url = filePath }));
        }