/// <summary> /// Gets all info about meeting protocols from the database and returns a partial view containing a table of /// this info. /// </summary> /// <returns>The rendered partial view.</returns> public ActionResult GetLgProtocols() { IEnumerable<LgProtocolViewModel> viewModels; using (IProtocolRepository repository = new ProtocolRepository()) { List<LgProtocolModel> models = repository.GetLgProtocols().ToList(); viewModels = models.Select(Mapper.Map<LgProtocolViewModel>); } return PartialView("Protocol/_LgProtocol", viewModels); }
/// <summary> /// Downloads the protocol with the given id as a pdf-file or returns the user /// to the view with the table of info about meeting protocols if something went wrong. /// </summary> /// <param name="id">The id of the meeting protocol-file to download.</param> /// <returns>redirects the user to the table with info about meeting protcols /// if something went wrong, otherwise returns a file-download-prompt.</returns> /// <exception cref="ArgumentException">Thrown if the method is called without an id.</exception> public ActionResult DownloadFile(int? id) { //if no id is provided, an exception is thrown. if (!id.HasValue) { throw new ArgumentException("id must be provided!"); } LgProtocolModel model; //Fetches info about the meeting protocol based on its id from the database . using (IProtocolRepository repository = new ProtocolRepository()) { model = repository.GetLgProtocolById(id.Value); } //Gets the original file name from the model with info fetched from the database string fileName = model.Name + ".pdf"; //Gets the path to the file on the server. string filePath = Server.MapPath("~/App_Data/LgProtocols") + "/" + model.NameOnServer; try { //Loads the file i´nto memory. byte[] fileData = System.IO.File.ReadAllBytes(filePath); //Gets the extension of the file based on the mimetype. string contentType = MimeMapping.GetMimeMapping(".pdf"); /*Creates a contentdisposition header and appends it to the http-response. It is needed for a download prompt to be generated.*/ var contentDisposition = new ContentDisposition { FileName = fileName, Inline = false }; Response.AppendHeader("Content-Disposition", contentDisposition.ToString()); return File(fileData, contentType); } catch { //if an error occured return teh user tot he view with the table of info abóut meeting protocols. return RedirectToAction("Index"); } }
public ActionResult UploadFile(LgProtocolFileViewModel viewModel) { HttpPostedFileBase file = viewModel.File; //checks if the file contains anything. if (file.ContentLength > 0) { string md5Hash; using (var md5 = MD5.Create()) { //Converts the md5hash to a string without "-". md5Hash = BitConverter.ToString(md5.ComputeHash(file.InputStream)).Replace("-", "").ToLower(); } var uploadDate = DateTime.Now; //Creates the name by concatenating the md5hash with the current date without "-". string nameOnServer = md5Hash + uploadDate.ToString("d").Replace("-", ""); //Converts the relative filepath to the absolute filepath. string filePath = Path.Combine(Server.MapPath("~/App_Data/LgProtocols"), nameOnServer); //Creates a model with info about the file that will later be saved to the database. var model = new LgProtocolModel { Name = Path.GetFileNameWithoutExtension(file.FileName), FileSize = file.ContentLength / 1024, MD5Hash = md5Hash, NameOnServer = nameOnServer, UploadDate = uploadDate }; //Saves the info to the database if it isn't uploaded allready. if (!System.IO.File.Exists(filePath)) { using (IProtocolRepository repository = new ProtocolRepository()) { repository.AddLgProtocol(model); repository.Save(); } //Saves the file to the server. file.SaveAs(filePath); } return Json(new { success = true }, JsonRequestBehavior.AllowGet); } //if the file was empty an error is returned. return Json(new { ErrorMessage = "File was empty" }, JsonRequestBehavior.AllowGet); }