您的位置:首页 > 其它

通过Web Services上传和下载文件

2009-02-12 09:23 393 查看
随着Internet技术的发展和跨平台需求的日益增加,Web Services的应用越来越广,我们不但需要通过Web Services传递字符串信息,而且需要传递二进制文件信息。下面,我就分别介绍如何通过Web Services从服务器下载文件到客户端和从客户端通过Web Services上载文件到服务器。

  一:通过Web Services显示和下载文件

  我们这里建立的Web Services的名称为GetBinaryFile,提供两个公共方法:分别是GetImage()和GetImageType(),前者返回二进制文件字节数组,后者返回文件类型,其中,GetImage()方法有一个参数,用来在客户端选择要显示或下载的文件名字。这里我们所显示和下载的文件可以不在虚拟目录下,采用这个方法的好处是:可以根据权限对文件进行显示和下载控制,从下面的方法我们可以看出,实际的文件位置并没有在虚拟目录下,因此可以更好地对文件进行权限控制,这在对安全性有比较高的情况下特别有用。这个功能在以前的ASP程序中可以用Stream对象实现。为了方便读者进行测试,这里列出了全部的源代码,并在源代码里进行介绍和注释。

   /// <summary>
  /// Web 服务提供的方法,返回给定文件的字节数组。
  /// </summary>
  [WebMethod(Description="Web 服务提供的方法,返回给定文件的字节数组")]
  public byte[] GetImage(string requestFileName)
  {
   ///得到服务器端的一个图片
   ///如果你自己测试,注意修改下面的实际物理路径
   if(requestFileName == null || requestFileName == "")
   return getBinaryFile("E://getpic//xp.JPG");
   else
   return getBinaryFile("E://getpic//" + requestFileName);
  }

  /// <summary>
  /// getBinaryFile:返回所给文件路径的字节数组。
  /// </summary>
  /// <param name="filename"></param>
  /// <returns></returns>
  public byte[] getBinaryFile(string filename)
  {
   if(File.Exists(filename))
   {
   try
   {
    ///打开现有文件以进行读取。
    FileStream s = File.OpenRead(filename);
    return ConvertStreamToByteBuffer(s);
   }
   catch(Exception e)
   {
    return new byte[0];
   }
   }
   else
   {
   return new byte[0];
   }
  }
 /// <summary>
 /// ConvertStreamToByteBuffer:把给定的文件流转换为二进制字节数组。
 /// </summary>
 /// <param name="theStream"></param>
 /// <returns></returns>
  public byte[] ConvertStreamToByteBuffer(System.IO.Stream theStream)
  {
   int b1;
   System.IO.MemoryStream tempStream = new System.IO.MemoryStream();
   while((b1=theStream.ReadByte())!=-1)
   {
   tempStream.WriteByte(((byte)b1));
   }
   return tempStream.ToArray();
  }
   [WebMethod(Description="Web 服务提供的方法,返回给定文件类型。")]
   public string GetImageType()
   {
   ///这里只是测试,您可以根据实际的文件类型进行动态输出
   return "image/jpg";
   }
 [WebMethod(Description = "Web 服务提供的方法,返回是否文件上载成功与否。")]
 public string UploadFile(byte[] fs, string FileName)
 {
 try
 {
  ///定义并实例化一个内存流,以存放提交上来的字节数组。
  MemoryStream m = new MemoryStream(fs);
  ///定义实际文件对象,保存上载的文件。
  FileStream f = new FileStream(Server.MapPath(".") + "//"
  + FileName, FileMode.Create);
  ///把内内存里的数据写入物理文件
  m.WriteTo(f);
  Bitmap bm = null;
  bm = new Bitmap(f);
  bm.Save(Server.MapPath(".") + "//"
  + FileName+".JPEG");
  m.Close();
  f.Close();
  f = null;
  m = null;
  return "文件已经上传成功。";
 }
 catch (Exception ex)
 {
  return ex.Message;
 }
 } 
  }
  客户端代码片段:

  private void button1_Click(object sender, EventArgs e)
 {
  pic.Service p = new getanddownpic.pic.Service();
      ///定义并初始化文件对象;
  ///得到二进制文件字节数组;
  byte[] image = p.GetImage("");
  ///转换为支持存储区为内存的流
  System.IO.MemoryStream memStream = new System.IO.MemoryStream(image);
  ///定义并实例化Bitmap对象
  Bitmap bm = new Bitmap(memStream);
  ///根据不同的条件进行输出或者下载;
  this.pictureBox1.Image = bm;
 }

  private void button2_Click(object sender, EventArgs e)
 {
  openFileDialog1.InitialDirectory = "C:/";
  openFileDialog1.Filter = "All Files|*.*|Bitmaps|*.bmp|GIFs|*.gif|JPEGs|*.jpg";
  openFileDialog1.FilterIndex = 2;
  if (openFileDialog1.ShowDialog() == DialogResult.OK)
  {
  pictureBox1.Image = Image.FromFile(openFileDialog1.FileName);
  pictureBox1.SizeMode = PictureBoxSizeMode.CenterImage;
  pictureBox1.BorderStyle = BorderStyle.Fixed3D;

   }
 }

  private void button3_Click(object sender, EventArgs e)
 {
  Bitmap bm = (Bitmap)this.pictureBox1.Image;
  System.IO.MemoryStream memStream = new System.IO.MemoryStream();
  if (this.textBox1.Text != null)
  {
  try
  {
   bm.Save(memStream, System.Drawing.Imaging.ImageFormat.Jpeg);
   int size = (int)memStream.Length;
   ///处理上载的文件流信息。
   byte[] b = new byte[size];
   b = memStream.GetBuffer();
   memStream.Close();
   pic.Service up = new getanddownpic.pic.Service();
   string r = up.UploadFile(b, this.textBox1.Text);
   MessageBox.Show(r);
  }
  catch (Exception ex)
  {
   MessageBox.Show(ex.Message);
  }
  }
  else
  {
  MessageBox.Show("please input a file name");
  }
 }
内容来自用户分享和网络整理,不保证内容的准确性,如有侵权内容,可联系管理员处理 点击这里给我发消息