public HttpResponseMessage UploadEformZip() { try { var core = _coreHelper.GetCore(); int.TryParse(HttpContext.Current.Request.Form.Get("templateId"), out int templateId); if (templateId <= 0) { return(Request.CreateResponse(HttpStatusCode.BadRequest, "Invalid Request!")); } var saveFolder = Path.Combine(core.GetJasperPath(), Path.Combine("templates", templateId.ToString())); var zipArchiveFolder = Path.Combine(core.GetJasperPath(), Path.Combine("templates", Path.Combine("zip-archives", templateId.ToString()))); if (string.IsNullOrEmpty(saveFolder)) { return(Request.CreateResponse(HttpStatusCode.BadRequest, "Folder error")); } if (!Directory.Exists(saveFolder)) { Directory.CreateDirectory(saveFolder); } if (!Directory.Exists(zipArchiveFolder)) { Directory.CreateDirectory(zipArchiveFolder); } var files = HttpContext.Current.Request.Files; if (files.Count > 0) { var httpPostedFile = files[0]; if (httpPostedFile.ContentLength > 0) { var filePath = Path.Combine(zipArchiveFolder, Path.GetFileName(httpPostedFile.FileName)); var extractPath = Path.Combine(saveFolder); if (File.Exists(filePath)) { httpPostedFile.SaveAs(filePath); } if (File.Exists(filePath)) { if (!Directory.Exists(extractPath)) { Directory.CreateDirectory(extractPath); } else { FoldersHelper.ClearFolder(extractPath); } ZipFile.ExtractToDirectory(filePath, extractPath); return(Request.CreateResponse(HttpStatusCode.OK)); } } } return(Request.CreateResponse(HttpStatusCode.BadRequest, "Invalid Request!")); } catch (Exception) { return(new HttpResponseMessage(HttpStatusCode.InternalServerError)); } }
public HttpResponseMessage UploadEformZip() { try { var core = _coreHelper.GetCore(); int.TryParse(HttpContext.Current.Request.Form.Get("templateId"), out int templateId); if (templateId <= 0) { return(Request.CreateResponse(HttpStatusCode.BadRequest, "Invalid Request!")); } var saveFolder = Path.Combine(core.GetJasperPath(), Path.Combine("templates", templateId.ToString())); var zipArchiveFolder = Path.Combine(core.GetJasperPath(), Path.Combine("templates", Path.Combine("zip-archives", templateId.ToString()))); if (string.IsNullOrEmpty(saveFolder)) { return(Request.CreateResponse(HttpStatusCode.BadRequest, "Folder error")); } Directory.CreateDirectory(saveFolder); Directory.CreateDirectory(zipArchiveFolder); var files = HttpContext.Current.Request.Files; if (files.Count > 0) { var httpPostedFile = files[0]; if (httpPostedFile.ContentLength > 0) { var filePath = Path.Combine(zipArchiveFolder, Path.GetFileName(httpPostedFile.FileName)); var extractPath = Path.Combine(saveFolder); if (File.Exists(filePath)) { File.Delete(filePath); } httpPostedFile.SaveAs(filePath); if (File.Exists(filePath)) { Directory.CreateDirectory(extractPath); FoldersHelper.ClearFolder(extractPath); using (var zip = ZipFile.Read(filePath)) { foreach (var entry in zip.Entries) { if (entry.FileName.Contains(".png") || entry.FileName.Contains("jrxml")) { entry.Extract(extractPath); } } } File.Delete(filePath); _coreHelper.bus.SendLocal(new GenerateJasperFiles(templateId)); return(Request.CreateResponse(HttpStatusCode.OK)); } } } return(Request.CreateResponse(HttpStatusCode.BadRequest, LocaleHelper.GetString("InvalidRequest"))); } catch (Exception ex) { return(new HttpResponseMessage(HttpStatusCode.InternalServerError)); } }