public ActionResult UploadDataNew() { if (Request.Files == null || Request.Files.Count == 0) { return(Json(new { type = false, message = "请选择文件" }, JsonRequestBehavior.AllowGet)); } HttpPostedFileBase fileSave = Request.Files[0]; if (fileSave == null) { return(Json(new { success = false, message = "请选择文件" }, JsonRequestBehavior.AllowGet)); } var instFile = new InstFile(); instFile.RefreshId(); instFile.ContentType = fileSave.ContentType; var newName = instFile.Id + '.' + fileSave.FileName.Substring(fileSave.FileName.LastIndexOf('.') + 1).ToLower(); string uri = "ftp://10.0.0.25" + "/" + newName; instFile.Name = fileSave.FileName; instFile.Path = uri; // 根据uri创建FtpWebRequest对象 var reqFtp = (FtpWebRequest)WebRequest.Create(new Uri(uri)); // ftp用户名和密码 reqFtp.Credentials = new NetworkCredential("shalu/jiajing.yang", "Passw0rd"); // 默认为true,连接不会被关闭 // 在一个命令之后被执行 reqFtp.KeepAlive = false; // 指定执行什么命令 reqFtp.Method = WebRequestMethods.Ftp.UploadFile; // 指定数据传输类型 reqFtp.UseBinary = true; // 上传文件时通知服务器文件的大小 reqFtp.ContentLength = fileSave.ContentLength; byte[] buff = new byte[fileSave.ContentLength]; // 把上传的文件写入流 Stream strm = reqFtp.GetRequestStream(); strm.Write(buff, 0, buff.Length); strm.Close(); InstFileRp.Insert(instFile); return(Json(new { type = true, fileId = instFile.Id }, JsonRequestBehavior.AllowGet)); }
public ActionResult Download(string fileId) { var file = InstFileRp.Get(fileId); if (file == null) { return(null); } var uri = (FileAddress.StartsWith("ftp://") ? FileAddress : "ftp://" + FileAddress) + "/" + file.Path; var reqFtp = (FtpWebRequest)WebRequest.Create(new Uri(uri)); reqFtp.Method = WebRequestMethods.Ftp.DownloadFile; reqFtp.UseBinary = true; reqFtp.Credentials = new NetworkCredential(FileAccount, FilePassword); FtpWebResponse response = (FtpWebResponse)reqFtp.GetResponse(); Stream ftpStream = response.GetResponseStream(); return(File(ftpStream, file.ContentType, file.Name)); }