using System;
using System.Data;
using System.Configuration;
using System.Web;
using System.Web.Security;
using System.Web.UI;
using System.Web.UI.WebControls;
using System.Web.UI.WebControls.WebParts;
using System.Web.UI.HtmlControls;
using System.Net;
using System.IO;
public partial class _Default : System.Web.UI.Page
{
string strServerPath = "http://192.168.1.101/OJSYS"; --------------OJSYS是虚拟目录的名称而不是对应物理目录的文件夹名称
string strLocalPath = "f:\\";
protected void Page_Load(object sender, EventArgs e)
{
}
protected void Button1_Click(object sender, EventArgs e)
{
string fullname = FileUpload1.FileName.ToString();//直接取得文件名
string url = FileUpload1.PostedFile.FileName.ToString();//取得上传文件路径
string typ1 = FileUpload1.PostedFile.ContentType.ToString();//获取文件MIME内容类型
string typ2 = fullname.Substring(fullname.LastIndexOf(".") + 1);//获取文件名字 . 后面的字符作为文件类型
if (FileUpload1.HasFile)
{
UpLoadFile(strLocalPath + fullname, strServerPath);
}
}
/// <summary>
/// WebClient上传文件至服务器(不带进度条)
/// </summary>
/// <param name="fileNameFullPath">要上传的文件(全路径格式)</param>
/// <param name="strUrlDirPath">Web服务器文件夹路径</param>
/// <returns>True/False是否上传成功</returns>
public bool UpLoadFile(string fileNameFullPath, string strUrlDirPath)
{
//得到要上传的文件文件名
string fileName = fileNameFullPath.Substring(fileNameFullPath.LastIndexOf("\\") + 1);
//新文件名由年月日时分秒及毫秒组成
string NewFileName = DateTime.Now.ToString("yyyyMMddhhmmss")
+ DateTime.Now.Millisecond.ToString()
+ fileNameFullPath.Substring(fileNameFullPath.LastIndexOf("."));
//得到文件扩展名
string fileNameExt = fileName.Substring(fileName.LastIndexOf(".") + 1);
if (strUrlDirPath.EndsWith("/") == false) strUrlDirPath = strUrlDirPath + "/";
//保存在服务器上时,将文件改名(示业务需要)
strUrlDirPath = strUrlDirPath + NewFileName;
// 创建WebClient实例
WebClient myWebClient = new WebClient();
myWebClient.Credentials = CredentialCache.DefaultCredentials;
myWebClient.Headers.Add("User-Agent", "Microsoft Internet Explorer");
// 将要上传的文件打开读进文件流
FileStream myFileStream = new FileStream(fileNameFullPath, FileMode.Open, FileAccess.Read);
BinaryReader myBinaryReader = new BinaryReader(myFileStream);
try
{
byte[] postArray = myBinaryReader.ReadBytes((int)myFileStream.Length);
//打开远程Web地址,将文件流写入
Stream postStream = myWebClient.OpenWrite(strUrlDirPath, "PUT");
if (postStream.CanWrite)
{
postStream.Write(postArray, 0, postArray.Length);
}
else
{
//MessageBox.Show("Web服务器文件目前不可写入,请检查Web服务器目录权限设置!","系统提示",MessageBoxButtons.OK,MessageBoxIcon.Information);
}
postStream.Close();//关闭流
return true;
}
catch (Exception exp)
{
//MessageBox.Show("文件上传失败:" + exp.Message, "系统提示", MessageBoxButtons.OK, MessageBoxIcon.Information);
return false;
}
}
//===============================================================================================================
public void downloadfile()
{
if (webClient.IsBusy)//是否存在正在进行中的Web请求
{
webClient.CancelAsync();
}
//为webClient添加事件
webClient.DownloadProgressChanged += new DownloadProgressChangedEventHandler(webClient_DownloadProgressChanged);
webClient.DownloadFileCompleted += new AsyncCompletedEventHandler(webClient_DownloadFileCompleted);
//开始下载
webClient.DownloadFileAsync(new Uri(this.textBox1.Text), "aa.rar");
}
}