利用web services 上传、下载文件
//file.asmx
/// <summary>
/// 返回文件名列表数组
/// </summary>
/// <returns></returns>
[WebMethod]
public string[] GetFileName()
{
return Directory.GetFiles(Server.MapPath("images"));
}
[WebMethod]
public byte[] GetFileByte(string filePath)
{
try
{
FileStream inputFile;
inputFile=File.Open(filePath,FileMode.Open,FileAccess.Read);
long fileLength=inputFile.Length;
byte[] dataBuffer=new Byte[fileLength];
inputFile.Read(dataBuffer,0,(int)fileLength);
inputFile.Close();
return dataBuffer;
}
catch (Exception ex)
{
throw(ex);
}
}
/// <summary>
/// 上传文件
/// </summary>
/// <returns></returns>
[WebMethod]
public Boolean UploadFile(byte[] fileSour,string fileName)
{
try
{
FileStream outinputFile;
outinputFile=File.Create(Server.MapPath("images").ToString() +"/" +fileName);
outinputFile.Write(fileSour,0,fileSour.Length);
outinputFile.Close();
return true;
}
catch(Exception ex)
{
throw(ex);
}
}
/// <summary>
/// 返回文件名列表数组
/// </summary>
/// <returns></returns>
[WebMethod]
public string[] GetFileName()
{
return Directory.GetFiles(Server.MapPath("images"));
}
[WebMethod]
public byte[] GetFileByte(string filePath)
{
try
{
FileStream inputFile;
inputFile=File.Open(filePath,FileMode.Open,FileAccess.Read);
long fileLength=inputFile.Length;
byte[] dataBuffer=new Byte[fileLength];
inputFile.Read(dataBuffer,0,(int)fileLength);
inputFile.Close();
return dataBuffer;
}
catch (Exception ex)
{
throw(ex);
}
}
/// <summary>
/// 上传文件
/// </summary>
/// <returns></returns>
[WebMethod]
public Boolean UploadFile(byte[] fileSour,string fileName)
{
try
{
FileStream outinputFile;
outinputFile=File.Create(Server.MapPath("images").ToString() +"/" +fileName);
outinputFile.Write(fileSour,0,fileSour.Length);
outinputFile.Close();
return true;
}
catch(Exception ex)
{
throw(ex);
}
}
//test.cs
/// <summary>
/// 应用程序的主入口点。
/// </summary>
[STAThread]
static void Main()
{
Application.Run(new GetFile());
}
/// <summary>
/// 加载数据
/// </summary>
/// <param name="sender"></param>
/// <param name="e"></param>
private void menuItem2_Click(object sender, System.EventArgs e)
{
try
{
this.Cursor=Cursors.WaitCursor;
this.Enabled=false;
this.statusBar1.Text="正在连接服务";
localhostWeb.file sw=new localhostWeb.file();
this.FileList.Visible=true;
this.FileList.DataSource=sw.GetFileName();
this.menuItem4.Enabled=true;
this.statusBar1.Text="完成.";
}
catch
{
this.menuItem4.Enabled=false;
this.statusBar1.Text="无法连接接服务,请确保网络通畅。";
}
finally
{
this.Cursor=Cursors.Default;
this.Enabled=true;
}
}
private void menuItem4_Click(object sender, System.EventArgs e)
{
this.saveFileDialog1.Filter="JPeg Image|*.jpg|Bitmap Image|*.bmp|Gif Image|*.gif";
this.saveFileDialog1.Title="保存图片";
this.saveFileDialog1.ShowDialog();
}
private void menuItem5_Click(object sender, System.EventArgs e)
{
openFileDialog1.ShowDialog();
}
private void openFileDialog1_FileOk(object sender, System.ComponentModel.CancelEventArgs e)
{
try
{
saveFilePath=this.openFileDialog1.FileName;
if (saveFilePath.Length > 0)
{
saveFilePath=saveFilePath.Substring((int)saveFilePath.LastIndexOf("\\") + 1);
this.Cursor=Cursors.WaitCursor;
this.Enabled=false;
FileStream openFile;
openFile=File.Open(saveFilePath,FileMode.Open,FileAccess.Read);
long fileLength=openFile.Length;
byte[] fileByte=new byte[fileLength];
openFile.Read(fileByte,0,(int)fileLength);
localhostWeb.file sw=new localhostWeb.file();
if (sw.UploadFile(fileByte,saveFilePath)==true)
{
MessageBox.Show("上传文件成功! ","上传文件",0,MessageBoxIcon.Information);
}
}
}
catch(Exception ex)
{
MessageBox.Show("文件保存时出现错误:\n\r"+ex.Message+"!","保存文件失败",0,MessageBoxIcon.Stop);
}
finally
{
this.Cursor=Cursors.Default;
this.Enabled=true;
}
}
private void saveFileDialog1_FileOk(object sender, System.ComponentModel.CancelEventArgs e)
{
//保存文件到本地
if (saveFileDialog1.FileName != "")
{
saveFilePath=saveFileDialog1.FileName;
try
{
this.Cursor=Cursors.WaitCursor;
this.Enabled=false;
localhostWeb.file sw=new localhostWeb.file();
byte[] imageFile;
FileStream outputFile;
imageFile=sw.GetFileByte(this.FileList.SelectedValue.ToString());
outputFile=File.Open(saveFilePath,FileMode.Create,FileAccess.Write);
outputFile.Write(imageFile,0,imageFile.Length);
outputFile.Close();
MessageBox.Show("文件保存成功! ","保存文件",0,MessageBoxIcon.Information);
}
catch(Exception ex)
{
MessageBox.Show("文件保存成功时出现错误:\n\r"+ex.Message+"!","保存文件失败",0,MessageBoxIcon.Stop);
}
finally
{
this.Cursor=Cursors.Default;
this.Enabled=true;
}
}
}
/// <summary>
/// 应用程序的主入口点。
/// </summary>
[STAThread]
static void Main()
{
Application.Run(new GetFile());
}
/// <summary>
/// 加载数据
/// </summary>
/// <param name="sender"></param>
/// <param name="e"></param>
private void menuItem2_Click(object sender, System.EventArgs e)
{
try
{
this.Cursor=Cursors.WaitCursor;
this.Enabled=false;
this.statusBar1.Text="正在连接服务";
localhostWeb.file sw=new localhostWeb.file();
this.FileList.Visible=true;
this.FileList.DataSource=sw.GetFileName();
this.menuItem4.Enabled=true;
this.statusBar1.Text="完成.";
}
catch
{
this.menuItem4.Enabled=false;
this.statusBar1.Text="无法连接接服务,请确保网络通畅。";
}
finally
{
this.Cursor=Cursors.Default;
this.Enabled=true;
}
}
private void menuItem4_Click(object sender, System.EventArgs e)
{
this.saveFileDialog1.Filter="JPeg Image|*.jpg|Bitmap Image|*.bmp|Gif Image|*.gif";
this.saveFileDialog1.Title="保存图片";
this.saveFileDialog1.ShowDialog();
}
private void menuItem5_Click(object sender, System.EventArgs e)
{
openFileDialog1.ShowDialog();
}
private void openFileDialog1_FileOk(object sender, System.ComponentModel.CancelEventArgs e)
{
try
{
saveFilePath=this.openFileDialog1.FileName;
if (saveFilePath.Length > 0)
{
saveFilePath=saveFilePath.Substring((int)saveFilePath.LastIndexOf("\\") + 1);
this.Cursor=Cursors.WaitCursor;
this.Enabled=false;
FileStream openFile;
openFile=File.Open(saveFilePath,FileMode.Open,FileAccess.Read);
long fileLength=openFile.Length;
byte[] fileByte=new byte[fileLength];
openFile.Read(fileByte,0,(int)fileLength);
localhostWeb.file sw=new localhostWeb.file();
if (sw.UploadFile(fileByte,saveFilePath)==true)
{
MessageBox.Show("上传文件成功! ","上传文件",0,MessageBoxIcon.Information);
}
}
}
catch(Exception ex)
{
MessageBox.Show("文件保存时出现错误:\n\r"+ex.Message+"!","保存文件失败",0,MessageBoxIcon.Stop);
}
finally
{
this.Cursor=Cursors.Default;
this.Enabled=true;
}
}
private void saveFileDialog1_FileOk(object sender, System.ComponentModel.CancelEventArgs e)
{
//保存文件到本地
if (saveFileDialog1.FileName != "")
{
saveFilePath=saveFileDialog1.FileName;
try
{
this.Cursor=Cursors.WaitCursor;
this.Enabled=false;
localhostWeb.file sw=new localhostWeb.file();
byte[] imageFile;
FileStream outputFile;
imageFile=sw.GetFileByte(this.FileList.SelectedValue.ToString());
outputFile=File.Open(saveFilePath,FileMode.Create,FileAccess.Write);
outputFile.Write(imageFile,0,imageFile.Length);
outputFile.Close();
MessageBox.Show("文件保存成功! ","保存文件",0,MessageBoxIcon.Information);
}
catch(Exception ex)
{
MessageBox.Show("文件保存成功时出现错误:\n\r"+ex.Message+"!","保存文件失败",0,MessageBoxIcon.Stop);
}
finally
{
this.Cursor=Cursors.Default;
this.Enabled=true;
}
}
}