protected bool __ie_response(Guid?applicationId, HttpPostedFile uploadedFile, DocFileInfo fi, ref string responseText, ref byte[] fileContent, bool dontStore = false) { string filename = HttpUtility.UrlDecode(uploadedFile.FileName); int indexOfLastDot = filename.LastIndexOf('.'); if (indexOfLastDot >= 0 && indexOfLastDot < filename.Length - 1) { fi.Extension = filename.Substring(indexOfLastDot + 1).ToLower(); } fi.FileName = string.IsNullOrEmpty(fi.Extension) ? filename : filename.Substring(0, indexOfLastDot); try { fi.Size = uploadedFile.ContentLength; using (BinaryReader reader = new BinaryReader(uploadedFile.InputStream)) { fileContent = reader.ReadBytes(uploadedFile.ContentLength); if (!dontStore) { fi.store(applicationId, fileContent); } } responseText = "{\"success\":" + true.ToString().ToLower() + ",\"AttachedFile\":" + fi.toJson(applicationId, true) + ",\"name\":\"" + fi.file_name_with_extension + "\"" + ",\"url\":\"" + fi.url(applicationId) + "\"" + ",\"Message\":\"~[[MSG]]\"}"; } catch (Exception) { fi.FileID = null; responseText = "{\"success\":\"false\"}"; return(false); } return(false); }
protected bool __other_browsers_response(Guid?applicationId, Stream inputStream, DocFileInfo fi, ref string responseText, ref byte[] fileContent, bool dontStore = false) { //This work for Firefox and Chrome. try { Stream st = HttpContext.Current.Request.Files.Count > 0 ? HttpContext.Current.Request.Files[0].InputStream : inputStream; using (BinaryReader reader = new BinaryReader(st)) { fileContent = reader.ReadBytes(Convert.ToInt32(st.Length)); if (!dontStore && !fi.store(applicationId, fileContent)) { responseText = "{\"success\":false}"; return(false); } } responseText = "{\"success\":" + true.ToString().ToLower() + ",\"AttachedFile\":" + fi.toJson(applicationId, true) + ",\"name\":\"" + fi.file_name_with_extension + "\"" + ",\"url\":\"" + fi.url(applicationId) + "\"" + ",\"Message\":\"~[[MSG]]\"}"; } catch (Exception) { fi.FileID = null; responseText = "{\"success\":false}"; return(false); } finally { inputStream.Close(); inputStream.Dispose(); } return(true); }
protected void send_file(DocFileInfo file, bool logNeeded, bool addPDFCover = false, bool addPDFFooter = false, Guid?coverId = null, string pdfPassword = null, string contentType = null, bool isAttachment = true) { byte[] fileContent = file.toByteArray(paramsContainer.ApplicationID); if (fileContent.Length == 0) { send_empty_response(); return; } //Save Log if (logNeeded && paramsContainer.CurrentUserID.HasValue) { LogController.save_log(paramsContainer.Tenant.Id, new Log() { UserID = paramsContainer.CurrentUserID, Date = DateTime.Now, HostAddress = PublicMethods.get_client_ip(HttpContext.Current), HostName = PublicMethods.get_client_host_name(HttpContext.Current), Action = Modules.Log.Action.Download, SubjectID = file.FileID, Info = file.toJson(paramsContainer.Tenant.Id), ModuleIdentifier = ModuleIdentifier.DCT }); } //end of Save Log if (file.Extension.ToLower() == "pdf") { addPDFCover = addPDFCover && file.OwnerNodeID.HasValue && coverId.HasValue && paramsContainer.ApplicationID.HasValue && paramsContainer.CurrentUserID.HasValue; if (addPDFFooter || addPDFCover) { bool invalidPassword = false; fileContent = PDFUtil.get_pdf_content(paramsContainer.Tenant.Id, fileContent, pdfPassword, ref invalidPassword); if (invalidPassword) { string responseText = "{\"InvalidPassword\":" + true.ToString().ToLower() + "}"; paramsContainer.return_response(ref responseText); return; } } if (addPDFFooter) { User currentUser = !paramsContainer.CurrentUserID.HasValue ? null : UsersController.get_user(paramsContainer.Tenant.Id, paramsContainer.CurrentUserID.Value); if (currentUser == null) { currentUser = new User() { UserID = Guid.NewGuid(), UserName = "******", FirstName = "[anonymous]", LastName = "[anonymous]" }; } DownloadedFileMeta meta = new DownloadedFileMeta(PublicMethods.get_client_ip(HttpContext.Current), currentUser.UserName, currentUser.FirstName, currentUser.LastName, null); fileContent = PDFTemplates.append_footer(fileContent, meta.toString()); } if (addPDFCover) { fileContent = Wiki2PDF.add_custom_cover(paramsContainer.Tenant.Id, paramsContainer.CurrentUserID.Value, fileContent, coverId.Value, file.OwnerNodeID.Value); } if (!string.IsNullOrEmpty(pdfPassword)) { fileContent = PDFUtil.set_password(fileContent, pdfPassword); } } string retFileName = file.FileName + (string.IsNullOrEmpty(file.Extension) ? string.Empty : "." + file.Extension); paramsContainer.file_response(fileContent, retFileName, contentType: contentType, isAttachment: isAttachment); }