永发信息网

怎样用.net实现FTP上传浏览器端文件

答案:1  悬赏:10  手机版
解决时间 2021-03-17 13:53
怎样用.net实现FTP上传浏览器端文件
最佳答案
///
/// FtpWeb 的摘要说明
///

public class FtpWeb
{
string ftpServerIP;
string ftpRemotePath;
string ftpUserID;
string ftpPassword;
string ftpURI;

///
/// 连接FTP
///

/// FTP连接地址
/// 指定FTP连接成功后的当前目录, 如果不指定即默认为根目录
/// 用户名
/// 密码
public FtpWeb(string FtpServerIP, string FtpRemotePath, string FtpUserID, string FtpPassword)
{
ftpServerIP = FtpServerIP;
ftpRemotePath = FtpRemotePath;
ftpUserID = FtpUserID;
ftpPassword = FtpPassword;
ftpURI = "ftp://" + ftpServerIP + "/" + ftpRemotePath + "/";
}

///
/// 上传
///

///
public bool Upload(string filename, out string errorMsg)
{
errorMsg = "";
FileInfo fileInf = new FileInfo(filename);
string uri = ftpURI + fileInf.Name;
FtpWebRequest reqFTP;

reqFTP = (FtpWebRequest)FtpWebRequest.Create(new Uri(uri));
reqFTP.Credentials = new NetworkCredential(ftpUserID, ftpPassword);
reqFTP.KeepAlive = false;
reqFTP.Method = WebRequestMethods.Ftp.UploadFile;
reqFTP.UseBinary = true;
reqFTP.ContentLength = fileInf.Length;
int buffLength = 2048; //开辟2KB缓存区
byte[] buff = new byte[buffLength];
int contentLen;
FileStream fs = fileInf.OpenRead();
try
{
Stream strm = reqFTP.GetRequestStream();
contentLen = fs.Read(buff, 0, buffLength);
while (contentLen != 0)
{
strm.Write(buff, 0, contentLen);
contentLen = fs.Read(buff, 0, buffLength);
}
strm.Close();
fs.Close();
return true;
}
catch (Exception ex)
{
errorMsg = ex.Message;
return false;
}
}

///
/// 下载
///

///
///
public bool Download(string filePath, string fileName, out string errorMsg)
{
errorMsg = "";
FtpWebRequest reqFTP;
try
{
FileStream outputStream = new FileStream(filePath + "//" + fileName, FileMode.Create);

reqFTP = (FtpWebRequest)FtpWebRequest.Create(new Uri(ftpURI + fileName));
reqFTP.Method = WebRequestMethods.Ftp.DownloadFile;
reqFTP.UseBinary = true;
reqFTP.Credentials = new NetworkCredential(ftpUserID, ftpPassword);
FtpWebResponse response = (FtpWebResponse)reqFTP.GetResponse();
Stream ftpStream = response.GetResponseStream();
long cl = response.ContentLength;
int bufferSize = 2048;
int readCount;
byte[] buffer = new byte[bufferSize];

readCount = ftpStream.Read(buffer, 0, bufferSize);
while (readCount > 0)
{
outputStream.Write(buffer, 0, readCount);
readCount = ftpStream.Read(buffer, 0, bufferSize);
}

ftpStream.Close();
outputStream.Close();
response.Close();
return true;
}
catch (Exception ex)
{
errorMsg = ex.Message;
return false;
}
}
}
我要举报
如以上问答信息为低俗、色情、不良、暴力、侵权、涉及违法等信息,可以点下面链接进行举报!
大家都在看
西安的五环体育有没有泳衣卖?
1997年11月21日是不是纯阴年
怎样给鲜花去刺
雷字开头的成语有哪些
怎么利用生鲜配送系统做好餐馆食材配送?
愿有岁月可回首 且以深情到白头表达的感情
苏州市相城区农业局北桥动物防疫站在什么地方
周公解梦梦见朋友拿衣服到家来卖
vivo闪充必须原装数据线吗?
励志的短语十四个字
鲤鱼炖出来的汤是什么颜色的
昆山张浦镇的打工楼在哪,和张浦的新昆山人居
如何计算现场的npsh值是多少
冰棒,雪糕,冰激凌有什么区别
你好,我的以前微信号是手机号码绑定的现在被
推荐资讯
男人剪毛好吗 男生剪毛下身会怎么样
我一年工资才四万多点,两个小孩上学就要花二
如何在一星期内使脸瘦下来?
我是学软件开发的新生 学校叫考证 考九门证书
海尔双王子冰箱的容积是多少
日本剑道实用么?
怒江之战郑恺演的谁
14岁男生,0基础IT知识,可以做什么软件
请根据我的性格取一个优雅的英文名(谢谢)
明天我要见女朋友最后一面了,该怎么样留住她?
请问江苏阜宁天宇华庄目前的房价多少?
pony怎么读
正方形一边上任一点到这个正方形两条对角线的
阴历怎么看 ?