且构网

分享程序员开发的那些事...
且构网 - 分享程序员编程开发的那些事

C# 本地文件夹上传至网络服务器中(待续)

更新时间:2022-09-14 18:23:11

 

一、文件的上传参考

思想,C#FTP上传

1
2
3
4
5
6
7
8
9
10
11
12
13
14
15
16
17
18
19
20
21
22
23
24
25
26
27
28
29
30
31
32
33
34
35
36
37
38
/// <summary>
/// 上传
/// </summary>
/// <param name="filename">要上传的本地文件名</param>
public void Upload(string filename)
{
    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.Proxy = null;
    reqFTP.Method = WebRequestMethods.Ftp.UploadFile;
    reqFTP.UseBinary = true;
    reqFTP.ContentLength = fileInf.Length;
    int buffLength = 2048;
    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();
    }
    catch (Exception ex)
    {
        Error_Log("FTP上传文件时发成错误,详细错误参数请查看错误日志。""Upload Error --> " + ex.Message + "\r\n" + ex.StackTrace);
    }
}

 

没有整理与归纳的知识,一文不值!高度概括与梳理的知识,才是自己真正的知识与技能。 永远不要让自己的***、好奇、充满创造力的想法被现实的框架所束缚,让创造力***成长吧! 多花时间,关心他(她)人,正如别人所关心你的。理想的腾飞与实现,没有别人的支持与帮助,是万万不能的。


    本文转自wenglabs博客园博客,原文链接:http://www.cnblogs.com/arxive/p/6069530.html,如需转载请自行联系原作者