using System.IO;
//调用文件列表
[WebMethod]
public string[] callfiles()
{
string[] Files;
Files = Directory.GetFiles("C:""myfile");
//设法把路径去掉
for (int i=0;i<Files.Length;i++)
{
string S=Files[i];
int d = S.IndexOf("""");
while (d > 0)
{
S = S.Substring(d + 1);
d = S.IndexOf("""");
}
Files[i]=S;
}
return Files;
}
//下载文件
[WebMethod]
public byte[] seefile(string filename)
{
byte[] rawData=new byte[0];
string fl="C:""myfile"""+filename;
try
{
//构造一个流对象,文件名是选中的文件
FileStream fs = new FileStream(fl, FileMode.OpenOrCreate, FileAccess.Read);
//构造一个动态字节数组
rawData= new byte[fs.Length];
//读流对象,从0开始,长度是fs的长度
fs.Read(rawData, 0, System.Convert.ToInt32(fs.Length));
fs.Close();
}
catch
{
}
return rawData;
}
//上传文件
[WebMethod]
public bool upload(byte[] filearray,string filename)
{
string S=filename;
//如果文件名存在路径,也应该去掉
int d = S.IndexOf("""");
while (d > 0)
{
S = S.Substring(d + 1);
d = S.IndexOf("""");
}
//构造一个本地的路径
filename="C:""myfile"""+S;
try
{
//构造一个文件流和一个二进制流
FileStream fs;
BinaryWriter bw;
//把数组写进文件
fs = new FileStream(filename, FileMode.OpenOrCreate, FileAccess.Write);
bw = new BinaryWriter(fs);
bw.Write(filearray);
bw.Close();
fs.Close();
return true;
}
catch
{
return false;
}
}
测试一下。