public FileStreamResult MachineFileClick(string CID, string MM_Idp, string number) { byte[] buffer = new byte[0]; string filename = "export.pdf"; string prefix = "pdf"; Dictionary <string, string> mimetypes = getMimeTypes(); try { int MM_Id = 0; int fileNumber = Convert.ToInt32(number); Int32.TryParse(MM_Idp, out MM_Id); if (CID.Length < 6) { throw new Exception("location variable incorrectly formatted."); } if (MM_Id == 0) { throw new Exception("Machine primary key cannot be zero."); } Helpers.CtxService service = new Helpers.CtxService(null, CID); List <ActiveMachineImgae> files = service.getmachineFiles(MM_Id); for (var i = 1; i <= files.Count; i++) { if (i == fileNumber) { buffer = files[i - 1].filebytes1; filename = files[i - 1].filename1; var splitarr = filename.Split('.'); if (splitarr.Length > 1) { prefix = splitarr[splitarr.Length - 1]; } break; } } } catch (Exception ex) { } if (buffer != null && mimetypes.ContainsKey(prefix.ToUpper())) { MemoryStream stream = new MemoryStream(buffer); stream.Flush(); stream.Position = 0; return(File(stream, mimetypes[prefix.ToUpper()], filename)); } else { MemoryStream stream = new MemoryStream(); stream.Flush(); stream.Position = 0; return(File(stream, "application/pdf", "export.pdf")); } }