예제 #1
0
 public JsonResult UploadOnlyImage(IFormFile fileinput)
 {
     try
     {
         var    files           = HttpContext.Request.Form.Files;
         var    file            = files.FirstOrDefault();
         var    fileName        = file.FileName;
         string fileContentType = file.ContentType.ToString();
         if (!(fileContentType == "image/bmp" || fileContentType == "image/gif" ||
               fileContentType == "image/png" || fileContentType == "image/x-png" || fileContentType == "image/jpeg" ||
               fileContentType == "image/pjpeg"))
         {
             return(Json(new { msg = "小主,只能上传图片", status = "false" }));
         }
         string filePath = Directory.GetCurrentDirectory() + @"\wwwroot\UploadImage";
         fileName = DateTime.Now.ToString("yyyyMMddHHmmss") + fileName;
         using (FileStream fs = new FileStream(filePath + "\\" + fileName, FileMode.Create))
         {
             file.CopyTo(fs);
         }
         string imageurl = "/UploadImage/" + fileName;
         WriteLogs.WriteLog("log.log", "exception", "111111111111111");
         return(Json(new { msg = fileName, status = "false" }, new Newtonsoft.Json.JsonSerializerSettings()));
     }
     catch (Exception ex)
     {
         WriteLogs.WriteLog("log.log", "exception", ex.Message);
         return(null);
     }
 }