还是我自己解决了!using System;
using System.IO;namespace UploadFile
{
interface IUploadFile
{
void UploadFile();//接口,上传文件。
}
public class TUploadFile:IUploadFile
{
string Src = "";
string Dest = "";
string DestName = "";
bool IsOverWrite = false;
public TUploadFile(string StrSrc)//一个参数,上传StrSrc到根目录下,保持原文件名。
{
this.Src = StrSrc;
this.Dest = @"./";
this.DestName = Path.GetFileName(this.Src);
this.IsOverWrite = false;
}
public TUploadFile(string StrSrc,string StrDes,string StrDesName,bool OverWrite)//参数分别是源文件名,目标地址,目标文件名,是否重写
{
this.Src = StrSrc;
this.Dest = StrDes;
this.DestName = StrDesName;
this.IsOverWrite = OverWrite;
}
public TUploadFile(string StrSrc,string StrDes,bool OverWrite)//保持原文件名
{
this.Src = StrSrc;
this.Dest = StrDes;
this.DestName = Path.GetFileName(this.Src);
this.IsOverWrite = OverWrite;
}
public TUploadFile(string StrSrc,string StrDes)
{
this.Src = StrSrc;
this.Dest = StrDes;
this.DestName = Path.GetFileName(this.Src);
this.IsOverWrite = false;
}
public string PDestName
{
get{return this.DestName;}
set{this.DestName = value;}
}
public string PSrc
{
get
{
return this.Src;
}
set
{
this.Src = value;
}
}
public string PDest
{
get{return this.Dest;}
set{this.Dest = value;}
}
public bool POverWrite
{
get{return this.IsOverWrite;}
set{this.IsOverWrite = value;}
}
public void UploadFile()
{
if (!this.IsOverWrite)
{
if(File.Exists(this.Src))
throw(new Exception("文件已经存在!"));
}
File.Copy(this.Src,System.Web.HttpContext.Current.Server.MapPath(this.Dest)+this.DestName,this.IsOverWrite);
}
}
}

解决方案 »

  1.   

    使用<input type="file">只是提供一个文件浏览,选取的方式,而webcontrol中可没有相应的控件 当然你可以自己做一个windows dialog,再调用;获取文件之后再把文件上传上去
      

  2.   

    呵呵,你怎么就不嫌繁呢?仅仅为了测试一个方法行不行,又是接口,又是重载,又是属性,写那么多的代码有必要吗?如果你的关键语句file.copy不行,岂不是白费力气?File.Copy可以在本地拷贝,上传的话可以使用webclient,直接使用它的upload方法就行了。
      

  3.   

    各位大哥请帮助。如何使用webclient。如何找
      

  4.   

    不是我不嫌繁,是公司的代码要求重用性好,我也觉得用File.Copy不行。谢谢各位提示
      

  5.   

    WebClient.UploadFile(string src,string method,string dst)中的method有几种方法?
      

  6.   

    公司的需求是这样的:通过目标服务器上的FTP服务器把本地文件上传到目标机器指定文件夹下。
      

  7.   

    1。UploadFile方法主要是通过http方式上传文件,它支持的uri默认的只有http,https,file三种方式,所以你说的ftp方式估计不适合。
    2。如果要把本地文件传到服务器上应该使用
       public byte[] UploadFile(string address,string fileName);
       方法。