急:谁能给我一个C#案例,关于 web 页面在ftp 上传下载的
答案:3 悬赏:10 手机版
解决时间 2021-08-17 20:12
- 提问者网友:杀生予夺
- 2021-08-17 05:45
最佳答案
- 五星知识达人网友:爱难随人意
- 2021-08-17 07:02
上传:
private string Upload(HttpPostedFile pf,string ExtName)
{
FtpSupport.FtpConnection ftp=new FtpSupport.FtpConnection();
System.IO.Stream fs=pf.InputStream;
string FileName=DateTime.Now.ToString( "yyyyMMddhhmmss ");
FileName+= ". "+ExtName;
ftp.Connect(ConfigurationSettings.AppSettings[ "FtpIP "].ToString(),ConfigurationSettings.AppSettings[ "FtpUserName "].ToString(),ConfigurationSettings.AppSettings[ "FtpPassWord "].ToString());
ftp.SetCurrentDirectory( "/ ");
ftp.PutStream(fs,FileName);
fs.Close();
ftp.Close();
return FileName;
}
当下载的时候:
private void FtpDown(string filename)
{
FtpSupport.FtpConnection ftp=new FtpSupport.FtpConnection();;
try
{
ftp.Connect(ConfigurationSettings.AppSettings[ "FtpIP "].ToString(),ConfigurationSettings.AppSettings[ "FtpUserName "].ToString(),ConfigurationSettings.AppSettings[ "FtpPassWord "].ToString());
try
{
ftp.SetCurrentDirectory( "/ ");
if(ftp.FileExist(filename)&&filename!= " ")
{
Response.Write( " <script> window.open( 'ftp:// "+Server.UrlEncode(ConfigurationSettings.AppSettings[ "FtpUserName "].ToString())+ ": "+ConfigurationSettings.AppSettings[ "FtpPassWord "].ToString()+ "@ "+ConfigurationSettings.AppSettings[ "FtpIP "].ToString()+ "/ "+filename+ " ') </script> ");
}
else
{
ClsOper.Alert( "Can Not Find File ");
}
}
catch
{
ClsOper.Alert( "Can Not Operate FTP ");
}
finally
{
ftp.Close();
}
}
catch
{
ClsOper.Alert( "Can Not Connect FTP ");
}
}
全部回答
最佳答案 WEB空间是用来放网页的,可以让别人浏览到,就像虚拟空间一样,一般服务器需要加装IIS或APACHE,而FTP空间则是用来存放文件专门供下载的,也就是说,FTP空间只能上传和下载,而不能通过IE访问。这种服务器不涉及到网站,一般只装SERVE-U就可以了。 采用的传输协议不一样,一个是HTTP,一个是FTP。 WEB上传与FTP上传的区别 WEB上传:即通过浏览器(IE)来上传文件 FTP上传:简称文件传输协议,通过FTP上传 1,通过IE浏览器上传文件,按照"操作向导"一步步操作完成,用户无须培训; 1,上传之前,需要安装专业上传软件,并对软件加以学习,用户需要学习上传软件; 2,通过分配用户权限发布课件,简单,安全; 2,需要建立FTP服务器及配置设置,专业性强; 3,支持断点续传,支持大文件上传; 3,不支持断点续传,只能重新上传,支持大文件上传; 4,上传课件属性(格式,上传时间,人员等)自动生成,方便快捷; 4,FTP上传后,需要从后台手工输入课件属性,费时费力; 5,上传后的课件,配有审核机制,保证课件质量; 5,FTP上传后的课件,没有审核机制; 6,审核后的课件,自动归类,用户通过校园网浏览; 6,FTP上传的课件后需要手工进行归类,比较烦麻;
- 2楼网友:woshuo
- 2021-08-17 07:37
public static void FileDownload(string FileName) { String FullFileName = System.Web.HttpContext.Current.Server.MapPath(FileName); FileInfo DownloadFile = new FileInfo(FullFileName); System.Web.HttpContext.Current.Response.Clear(); System.Web.HttpContext.Current.Response.ClearHeaders(); System.Web.HttpContext.Current.Response.Buffer = false; System.Web.HttpContext.Current.Response.ContentType = "application/octet-stream"; System.Web.HttpContext.Current.Response.AppendHeader("Content-Disposition", "attachment;filename=" + System.Web.HttpUtility.UrlEncode(DownloadFile.FullName, System.Text.Encoding.UTF8)); System.Web.HttpContext.Current.Response.AppendHeader("Content-Length", DownloadFile.Length.ToString()); System.Web.HttpContext.Current.Response.WriteFile(DownloadFile.FullName); System.Web.HttpContext.Current.Response.Flush(); System.Web.HttpContext.Current.Response.End(); } 下载,直接调用方法 try { if (FileUpload1.PostedFile.FileName.Length == 0) { Response.Write("<script>alert(\"上传路径不能为空!\");</script>"); return; } string filename = this.FileUpload1.PostedFile.FileName.Substring(this.FileUpload1.PostedFile.FileName.LastIndexOf("\\") + 1); string houzhui = FileUpload1.PostedFile.FileName.Substring(this.FileUpload1.PostedFile.FileName.LastIndexOf(".") + 1); if (houzhui == "gif" || houzhui == "jpg" || houzhui == "bmp") { FileUpload1.PostedFile.SaveAs(Server.MapPath("image/" + filename)); Response.Write("<script>alert(\"上传文件成功!\");</script>"); } else { Response.Write("<script>alert(\"上传文件必须是gif,jpg,bmp格式!\");</script>"); return; } } catch (Exception ex) { Response.Write("<script>alert(\"" + ex.Message + "\");</script>"); } 上传,根据自己需要稍加改动
我要举报
如以上回答内容为低俗、色情、不良、暴力、侵权、涉及违法等信息,可以点下面链接进行举报!
点此我要举报以上问答信息
大家都在看
推荐资讯