asp.net ftp 上传,下载,删除文件,创建文件夹,删除文夹,重命名类

asp.net ftp 上传,下载,删除文件,创建文件夹,删除文夹,重命名类

 

对于asp.net,默认只允许上传2M文件 fileupload的扩展

对于asp.net,默认只允许上传2M文件,在增加如下配置,一般可以自定义最大文件大小.

executionTimeout="300"

maxRequestLength="40960"

useFullyQualifiedRedirectUrl="false"/>

另外: 判断客户端文件大小

this.FileUpload1.PostedFile.ContentLength;
单位为字节KB

参数最大是2091151,2G左右大小

 

 

using System;
using System.Data;
using System.Configuration;
using System.Linq;
using System.Web;
using System.Web.Security;
using System.Web.UI;
using System.Web.UI.HtmlControls;
using System.Web.UI.WebControls;
using System.Web.UI.WebControls.WebParts;
using System.Xml.Linq;
using System.Net;
using System.IO;
using System.Web.Configuration;

namespace Webftp
{
    public sealed class FtpClientService
    {
        #region Internal Members

        private NetworkCredential certificate;

        #endregion

        ///


        /// 构造函数,提供初始化数据的功能,打开Ftp站点
        ///

        public FtpClientService()
        {
            certificate = new NetworkCredential(WebConfigurationManager.AppSettings["FtpUserName"], WebConfigurationManager.AppSettings["FtpPassword"]);
        }

        ///


        /// 创建FTP请求
        ///

        /// ftp://myserver/upload.txt
        /// Upload/Download
        ///
        private FtpWebRequest CreateFtpWebRequest(Uri uri, string method)
        {
            FtpWebRequest ftpClientRequest = (FtpWebRequest)WebRequest.Create(uri);

            ftpClientRequest.Proxy = null;
            ftpClientRequest.Credentials = certificate;
            ftpClientRequest.KeepAlive = true;
            ftpClientRequest.UseBinary = true;
            ftpClientRequest.UsePassive = true;
            ftpClientRequest.Method = method;
            ftpClientRequest.UsePassive = true;
            //ftpClientRequest.Timeout = -1;

            return ftpClientRequest;
        }
        #region 支持断点续传

        public bool UploadFile(string sourceFile, Uri destinationPath, int offSet, string ftpMethod)
        {
            try
            {
                FileInfo file = new FileInfo(sourceFile);
                Uri uri = new Uri(destinationPath.AbsoluteUri + "/" + file.Name);
                FtpWebRequest request = CreateFtpWebRequest(uri, ftpMethod);

                request.ContentOffset = offSet;
                Stream requestStream = request.GetRequestStream();//需要获取文件的流
                FileStream fileStream = new FileStream(sourceFile, FileMode.Open, FileAccess.Read);//创建存储文件的流
                int sourceLength = (int)fileStream.Length;
                offSet = CopyDataToDestination(fileStream, requestStream, offSet);
                WebResponse response = request.GetResponse();
                response.Close();
                if (offSet != 0)
                {
                    UploadFile(sourceFile, destinationPath, offSet, WebRequestMethods.Ftp.AppendFile);
                }
            }
            catch (Exception ex)
            {

                return false;
            }

            return true;
        }

        private int CopyDataToDestination(Stream sourceStream, Stream destinationStream, int offSet)
        {
            try
            {
                int sourceLength = (int)sourceStream.Length;
                int length = sourceLength - offSet;
                byte[] buffer = new byte[length + offSet];
                int bytesRead = sourceStream.Read(buffer, offSet, length);
                while (bytesRead != 0)
                {
                    destinationStream.Write(buffer, 0, bytesRead);
                    bytesRead = sourceStream.Read(buffer, 0, length);
                    length = length - bytesRead;
                    offSet = (bytesRead == 0) ? 0 : (sourceLength - length);//(length - bytesRead);                 
                }
            }
        

你可能感兴趣的:(asp.net ftp 上传,下载,删除文件,创建文件夹,删除文夹,重命名类)