public ActionResult Upload(Guid?Id, string message = null) { var ajaxMessage = new AjaxResponse { Success = true, Message = "上传成功!" }; var resoureType = db.Site_ResourceTypes.Find(Id); if (resoureType == null) { ajaxMessage.Success = false; ajaxMessage.Message = "目录不存在或已删除!"; return(Json(ajaxMessage)); } foreach (var fileKey in Request.Files.AllKeys) { var file = Request.Files[fileKey]; try { var uploadFileBytes = new byte[file.ContentLength]; try { file.InputStream.Read(uploadFileBytes, 0, file.ContentLength); } catch (Exception ex) { ajaxMessage.Success = false; ajaxMessage.Message = "文件写入错误!"; Logger.Log(Magicodes.Logger.LoggerLevels.Error, ex.ToString()); return(Json(ajaxMessage)); } if (file != null) { SiteResourceHelper.Upload(resoureType, Path.GetFileName(file.FileName), uploadFileBytes, db, out ajaxMessage); } } catch (Exception ex) { ajaxMessage.Success = false; ajaxMessage.Message = ex.Message; Logger.Log(Magicodes.Logger.LoggerLevels.Error, ex.ToString()); } } return(Json(ajaxMessage)); }
public object UploadImages() { byte[] uploadFileBytes = null; string uploadFileName = null; if (UploadConfig.Base64) { uploadFileName = UploadConfig.Base64Filename; uploadFileBytes = Convert.FromBase64String(Request[UploadConfig.UploadFieldName]); Result.State = UploadState.FileAccessError; Result.ErrorMessage = "不支持此格式上传"; return WriteResult(); } var file = HttpContext.Current.Request.Files[UploadConfig.UploadFieldName]; uploadFileName = file.FileName; if (!CheckFileType(uploadFileName)) { Result.State = UploadState.TypeNotAllow; return WriteResult(); } if (!CheckFileSize(file.ContentLength)) { Result.State = UploadState.SizeLimitExceed; return WriteResult(); } uploadFileBytes = new byte[file.ContentLength]; try { file.InputStream.Read(uploadFileBytes, 0, file.ContentLength); } catch (Exception) { Result.State = UploadState.NetworkError; return WriteResult(); } try { using (var db = new AppDbContext()) { var defaultType = db.Site_ResourceTypes.FirstOrDefault( p => p.IsSystemResource && (p.ResourceType == SiteResourceTypes.Gallery)); if (defaultType == null) defaultType = new Site_ResourceType { IsSystemResource = false, ResourceType = SiteResourceTypes.Gallery }; var ajaxMessage = new AjaxResponse {Success = true}; var fileBase = SiteResourceHelper.Upload(defaultType, Path.GetFileName(file.FileName), uploadFileBytes, db, out ajaxMessage); Result.OriginFileName = fileBase.Name; Result.Url = fileBase.SiteUrl; Result.State = UploadState.Success; return WriteResult(); } } catch (Exception) { Result.State = UploadState.NetworkError; return WriteResult(); } }