就是想要一个像写邮件时浏览附件一样的控件
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>"; Response.Write(jsBlock); Response.Flush(); return false; } int sourceLength = int.Parse(sourceStream.Length.ToString()); offSet = CopyDataToDestination(sourceStream, requestStream, offSet); WebResponse response = request.GetResponse(); response.Close(); } catch (Exception ex) { ex.ToString(); return false; } return true; } public string GetFileNames(string URI) { WebClient wClient = new WebClient(); FtpWebRequest FtpClient; Uri uri = new Uri(URI); FtpClient = CreateFtpWebRequest(uri, WebRequestMethods.Ftp.GetFileSize); try { FtpWebResponse response = (FtpWebResponse)FtpClient.GetResponse(); return response.StatusDescription; } catch (Exception ex) { return ""; } } private int CopyDataToDestination(Stream sourceStream, Stream destinationStream, int offSet) { try { int sourceLength = (int)sourceStream.Length; sourceStream.Seek(offSet, SeekOrigin.Current); string jsBlock; int bytesRead = 0; int iSpan = 1024 * 1024;//一次传1M if (sourceLength - offSet < iSpan) iSpan = sourceLength - offSet; byte[] buffer = new byte[iSpan]; bytesRead = sourceStream.Read(buffer, 0, iSpan); while (bytesRead != 0) { destinationStream.Write(buffer, 0, iSpan); offSet += iSpan; if (sourceLength - offSet < iSpan) iSpan = sourceLength - offSet; bytesRead = sourceStream.Read(buffer, 0, iSpan); int i = (int)((double)offSet / (double)sourceLength * 100f); jsBlock = "<script>SetPorgressBar('" + "A" + i.ToString() + "','" + i.ToString() + "');</script>"; Response.Write(jsBlock); Response.Flush(); } offSet = 0; } catch (Exception ex) { string jsBlock = "<script>alert('" + ex.Message + "');</script>"; Response.Write(jsBlock); Response.Flush(); string error = ex.ToString(); return offSet; } finally { sourceStream.Close(); destinationStream.Close(); } return offSet; } #endregion}