日期:2014-05-17  浏览次数:20430 次

哪位大师知道ASP.NET好的FTP上传控件啊
就是想要一个像写邮件时浏览附件一样的控件
1、支持进度条显示
2、支持断点续传
3、支持最少500M以下的文件上传
4、支持取消暂停上传

我自己也做了一个(支持进度条显示,支持断点续传,支持最少500M以下的文件上传),但进度条必须显示在一个弹出窗口。也不能暂停或者取消上传,由于自己写的很不稳定,有很多小问题。所以希望各位推荐个成熟的第三方控件

C# code

using System;
using System.Collections.Generic;
using System.Linq;
using System.Web;
using System.Web.UI;
using System.Web.UI.WebControls;
using System.ComponentModel;
using System.Data;
using System.Drawing;
using System.Text;
using System.Threading;
using System.IO;
using System.Net;
using System.Net.Security;

public partial class FTPUpload : System.Web.UI.Page
{
    private NetworkCredential certificate;
    private FtpBLL.FTPGlobalVar FTPVar;
    protected void Page_Load(object sender, EventArgs e)
    {
        FTPVar = FtpBLL.staticFTPGlobalVar.FTPVar;
        if (FTPVar != null)
        {
            UploadFile(FTPVar.Target, FTPVar.user, FTPVar.password, FTPVar.buffer);
            FtpBLL.staticFTPGlobalVar.FTPVar = null;
            GC.Collect();
        }
    }

    private bool UploadFile(string target, string userName, string password, byte[] buffer)
    {
        bool bReturn = false;
        // 根据 ProgressBar.htm 显示进度条界面
        string templateFileName = Path.Combine(Server.MapPath("."), "ProgressBar.htm");
        StreamReader reader = new StreamReader(@templateFileName, System.Text.Encoding.GetEncoding("gb2312"));
        string html = reader.ReadToEnd();
        reader.Close();
        Response.Write(html);
        Response.Flush();
        certificate = new NetworkCredential(userName, password);
        int fsLength = buffer.Length;
        MemoryStream sourceStreamT = new MemoryStream(buffer);
        
        Stream sourceStream = sourceStreamT;
        string message = GetFileNames(target);
        int x = 250000;
        int filemax = 0;

        if (message != "")
        {
            filemax = int.Parse(message.Substring(message.IndexOf(" ") + 1).Substring(0, message.Substring(message.IndexOf(" ") + 1).Length - 2));
        }
        if (filemax == fsLength)
        {
            string jsBlock = "<script>ShowMsg('该文件已完成上传。');</script>";
            Response.Write(jsBlock);
            Response.Flush();
            return true;
        }
        if (filemax != 0)
        {
            UploadFile(sourceStream, new Uri(target), filemax, WebRequestMethods.Ftp.UploadFile);
        }
        else
        {
            UploadFile(sourceStream, new Uri(target), 0, WebRequestMethods.Ftp.UploadFile);
        }
        return bReturn;
    }

    #region Internal Members

    #endregion

    /// <summary>
    /// 创建FTP请求
    /// </summary>
    private FtpWebRequest CreateFtpWebRequest(Uri uri, string method)
    {
        FtpWebRequest ftpClientRequest = (FtpWebRequest)WebRequest.Create(uri);
        ftpClientRequest.KeepAlive = false;
        ftpClientRequest.Proxy = null;
        ftpClientRequest.Credentials = certificate;
        ftpClientRequest.KeepAlive = true;
        ftpClientRequest.UseBinary = true;
        ftpClientRequest.UsePassive = true;
        ftpClientRequest.Method = method;
        //ftpClientRequest.Timeout = 60;
        return ftpClientRequest;
    }
    #region 支持断点续传

    public bool UploadFile(Stream sourceStream, Uri destinationPath, int offSet, string ftpMethod)
    {
        try
        {
            FtpWebRequest request = CreateFtpWebRequest(destinationPath, ftpMethod);

            request.ContentOffset = offSet;
            Stream requestStream = null;
            try
            {
                requestStream = request.GetRequestStream();//需要获取文件的流
            }
            catch(Exception ex)
            {
                string jsBlock = "<script>alert('" + ex.Message + "');</script>