public ActionResult DownloadFile(string fileCode, string fileName, string fileType) { try { FtpServer ftpServer = new FtpServerDao().GetAllFtpServerList().ToList()[0]; string host = ftpServer.ServerAddress; //string host = "ftp://localhost/"; string remoteFile = fileCode + "." + fileType; var username = ftpServer.UserName; var password = ftpServer.Password; var ftpRequest = (FtpWebRequest)FtpWebRequest.Create(host + "/" + remoteFile); ftpRequest.Credentials = new NetworkCredential(username, password); ftpRequest.UseBinary = true; ftpRequest.UsePassive = true; ftpRequest.KeepAlive = true; ftpRequest.Method = WebRequestMethods.Ftp.DownloadFile; var ftpResponse = (FtpWebResponse)ftpRequest.GetResponse(); var ftpStream = ftpResponse.GetResponseStream(); string contentType = "application/" + fileType; return(File(ftpStream, contentType, fileName)); } catch (Exception ex) { ViewBag.Message = "File Download failed!!"; return(View()); } }
public JsonResult UploadFile(IFormFile attachFile) { ReturnMessage returnMessage = new ReturnMessage(); FindPatient fnPatient = new FindPatient(); if (attachFile == null) { fnPatient.RetCode = 0; fnPatient.RetMessage = "Please select one file!"; return(Json(fnPatient)); } try { List <FtpServer> lstFtpServer = new FtpServerDao().GetAllFtpServerList().ToList(); if (lstFtpServer == null || lstFtpServer.Count < 1) { fnPatient.RetCode = 0; fnPatient.RetMessage = "Can not connect FTP Server!"; return(Json(fnPatient)); } string serverAddress = lstFtpServer[0].ServerAddress; string userName = lstFtpServer[0].UserName; string password = lstFtpServer[0].Password; var ftpRequest = (FtpWebRequest)FtpWebRequest.Create(serverAddress + "FTPTEST.txt"); ftpRequest.Credentials = new NetworkCredential(userName, password); ftpRequest.Method = WebRequestMethods.Ftp.DownloadFile; var ftpResponse = (FtpWebResponse)ftpRequest.GetResponse(); var ftpStream = ftpResponse.GetResponseStream(); Attachments attachment = new Attachments(); attachment.FileCode = ""; attachment.FileName = attachFile.FileName; int dotIndex = attachFile.FileName.LastIndexOf('.'); attachment.FileType = attachFile.FileName.Substring(dotIndex + 1); //User user = (User)Session["AppUser"]; attachment.AttachedBy = 1; //List<Attachments> lstAttachments = (List<Attachments>)Session["lstAttachments"]; List <Attachments> lstAttachments = new List <Attachments>(); returnMessage = new AttachmentDao().Create(attachment); if (returnMessage.RetCode > 0) { var attachFileName = returnMessage.RetMessage + "." + attachment.FileType; //var reader = new StreamReader(); Stream streamObj = attachFile.OpenReadStream(); byte[] buffer = new byte[attachFile.Length]; streamObj.Read(buffer, 0, buffer.Length); streamObj.Close(); streamObj = null; string ftpurl = String.Format("{0}/{1}", serverAddress, attachFileName); var requestObj = FtpWebRequest.Create(ftpurl) as FtpWebRequest; requestObj.Method = WebRequestMethods.Ftp.UploadFile; requestObj.Credentials = new NetworkCredential(userName, password); var requestStream = requestObj.GetRequestStream(); requestStream.Write(buffer, 0, buffer.Length); requestStream.Flush(); requestStream.Close(); requestObj = null; attachment.Id = returnMessage.RetCode; attachment.FileCode = returnMessage.RetMessage; fnPatient.RetCode = 1; lstAttachments.Add(attachment); } HttpContext.Session.SetObjectAsJson("lstAttachments", lstAttachments); fnPatient.AttachmentsList = lstAttachments; return(Json(fnPatient)); } catch (Exception ex) { fnPatient.RetCode = 0; fnPatient.RetMessage = "Error in file attachment"; return(Json(fnPatient)); } }