通过Web Services上传和下载图片文件

     通过Web Services上传和下载图片文件

    随着Internet技术的发展和跨平台需求的日益增加,Web Services的应用越来越广,我们不但需要通过Web Services传递字符串信息,而且需要传递二进制文件信息。下面,我就分别介绍如何通过Web Services从服务器下载文件到客户端和从客户端通过Web Services上载文件到服务器。
     我们这里建立的Web Services的名称为GetBinaryFile,提供两个公共方法:分别是GetImage()和GetImageType(),前者返回二进制文件字节数组,后者返回文件类型,其中,GetImage()方法有一个参数,用来在客户端选择要显示或下载的文件名字。这里我们所显示和下载的文件可以不在虚拟目录下,采用这个方法的好处是:可以根据权限对文件进行显示和下载控制,从下面的方法我们可以看出,实际的文件位置并没有在虚拟目录下,因此可以更好地对文件进行权限控制,这在对安全性有比较高的情况下特别有用。这个功能在以前的ASP程序中可以用Stream对象实现。为了方便读者进行测试,这里列出了全部的源代码,并在源代码里进行介绍和注释。
     

ExpandedBlockStart.gif ContractedBlock.gif   /**/ /// <summary>
InBlock.gif    
/// Web 服务提供的方法,返回给定文件的字节数组。
ExpandedBlockEnd.gif    
/// </summary>

None.gif     [WebMethod(Description = " Web 服务提供的方法,返回给定文件的字节数组 " )]
None.gif    
public   byte [] GetImage( string  requestFileName)
ExpandedBlockStart.gifContractedBlock.gif    
dot.gif {
ExpandedSubBlockStart.gifContractedSubBlock.gif     
/**////得到服务器端的一个图片
ExpandedSubBlockEnd.gif     
///如果你自己测试,注意修改下面的实际物理路径

InBlock.gif     if(requestFileName == null || requestFileName == "")
InBlock.gif      
return getBinaryFile("E:\\getpic\\xp.JPG");
InBlock.gif     
else
InBlock.gif      
return getBinaryFile("E:\\getpic\\" + requestFileName);
ExpandedBlockEnd.gif    }

None.gif
ExpandedBlockStart.gifContractedBlock.gif    
/**/ /// <summary>
InBlock.gif    
/// getBinaryFile:返回所给文件路径的字节数组。
InBlock.gif    
/// </summary>
InBlock.gif    
/// <param name="filename"></param>
ExpandedBlockEnd.gif    
/// <returns></returns>

None.gif      public   byte [] getBinaryFile( string  filename)
ExpandedBlockStart.gifContractedBlock.gif    
dot.gif {
InBlock.gif     
if(File.Exists(filename))
ExpandedSubBlockStart.gifContractedSubBlock.gif     
dot.gif{
InBlock.gif      
try
ExpandedSubBlockStart.gifContractedSubBlock.gif      
dot.gif{
ExpandedSubBlockStart.gifContractedSubBlock.gif       
/**////打开现有文件以进行读取。
InBlock.gif       FileStream s = File.OpenRead(filename);
InBlock.gif       
return ConvertStreamToByteBuffer(s);
ExpandedSubBlockEnd.gif      }

InBlock.gif      
catch(Exception e)
ExpandedSubBlockStart.gifContractedSubBlock.gif      
dot.gif{
InBlock.gif       
return new byte[0];
ExpandedSubBlockEnd.gif      }

ExpandedSubBlockEnd.gif     }

InBlock.gif     
else
ExpandedSubBlockStart.gifContractedSubBlock.gif     
dot.gif{
InBlock.gif      
return new byte[0];
ExpandedSubBlockEnd.gif     }

ExpandedBlockEnd.gif    }

ExpandedBlockStart.gifContractedBlock.gif  
/**/ /// <summary>
InBlock.gif  
/// ConvertStreamToByteBuffer:把给定的文件流转换为二进制字节数组。
InBlock.gif  
/// </summary>
InBlock.gif  
/// <param name="theStream"></param>
ExpandedBlockEnd.gif  
/// <returns></returns>

None.gif      public   byte [] ConvertStreamToByteBuffer(System.IO.Stream theStream)
ExpandedBlockStart.gifContractedBlock.gif    
dot.gif {
InBlock.gif     
int b1;
InBlock.gif     System.IO.MemoryStream tempStream 
= new System.IO.MemoryStream();
InBlock.gif     
while((b1=theStream.ReadByte())!=-1)
ExpandedSubBlockStart.gifContractedSubBlock.gif     
dot.gif{
InBlock.gif      tempStream.WriteByte(((
byte)b1));
ExpandedSubBlockEnd.gif     }

InBlock.gif     
return tempStream.ToArray();
ExpandedBlockEnd.gif    }

None.gif     [WebMethod(Description
= " Web 服务提供的方法,返回给定文件类型。 " )]
None.gif     
public   string  GetImageType()
ExpandedBlockStart.gifContractedBlock.gif     
dot.gif {
ExpandedSubBlockStart.gifContractedSubBlock.gif      
/**////这里只是测试,您可以根据实际的文件类型进行动态输出
InBlock.gif      return "image/jpg";
ExpandedBlockEnd.gif     }

None.gif [WebMethod(Description 
=   " Web 服务提供的方法,返回是否文件上载成功与否。 " )]
None.gif 
public   string  UploadFile( byte [] fs,  string  FileName)
ExpandedBlockStart.gifContractedBlock.gif 
dot.gif {
InBlock.gif  
try
ExpandedSubBlockStart.gifContractedSubBlock.gif  
dot.gif{
ExpandedSubBlockStart.gifContractedSubBlock.gif   
/**////定义并实例化一个内存流,以存放提交上来的字节数组。
InBlock.gif   MemoryStream m = new MemoryStream(fs);
ExpandedSubBlockStart.gifContractedSubBlock.gif   
/**////定义实际文件对象,保存上载的文件。
InBlock.gif   FileStream f = new FileStream(Server.MapPath("."+ "\\"
InBlock.gif    
+ FileName, FileMode.Create);
ExpandedSubBlockStart.gifContractedSubBlock.gif   
/**////把内内存里的数据写入物理文件
InBlock.gif   m.WriteTo(f);
InBlock.gif   Bitmap bm 
= null;
InBlock.gif   bm 
= new Bitmap(f);
InBlock.gif   bm.Save(Server.MapPath(
"."+ "\\"
InBlock.gif    
+ FileName+".JPEG");
InBlock.gif   m.Close();
InBlock.gif   f.Close();
InBlock.gif   f 
= null;
InBlock.gif   m 
= null;
InBlock.gif   
return "文件已经上传成功。";
ExpandedSubBlockEnd.gif  }

InBlock.gif  
catch (Exception ex)
ExpandedSubBlockStart.gifContractedSubBlock.gif  
dot.gif{
InBlock.gif   
return ex.Message;
ExpandedSubBlockEnd.gif  }

ExpandedBlockEnd.gif }

None.gif
None.gif
None.gif}
None.gif
None.gif


客户端代码片段:

None.gif private   void  button1_Click( object  sender, EventArgs e)
ExpandedBlockStart.gifContractedBlock.gif  
dot.gif {
InBlock.gif   pic.Service p 
= new getanddownpic.pic.Service();
ExpandedSubBlockStart.gifContractedSubBlock.gif            
/**////定义并初始化文件对象;
ExpandedSubBlockEnd.gif   
///得到二进制文件字节数组;

InBlock.gif   byte[] image = p.GetImage("");
ExpandedSubBlockStart.gifContractedSubBlock.gif   
/**////转换为支持存储区为内存的流
InBlock.gif   System.IO.MemoryStream memStream = new System.IO.MemoryStream(image);
ExpandedSubBlockStart.gifContractedSubBlock.gif   
/**////定义并实例化Bitmap对象
InBlock.gif   Bitmap bm = new Bitmap(memStream);
ExpandedSubBlockStart.gifContractedSubBlock.gif   
/**////根据不同的条件进行输出或者下载;
InBlock.gif   this.pictureBox1.Image = bm;
ExpandedBlockEnd.gif  }

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

ExpandedBlockEnd.gif  }

None.gif
None.gif  
private   void  button3_Click( object  sender, EventArgs e)
ExpandedBlockStart.gifContractedBlock.gif  
dot.gif {
InBlock.gif   Bitmap bm 
= (Bitmap)this.pictureBox1.Image;
InBlock.gif   System.IO.MemoryStream memStream 
= new System.IO.MemoryStream();
InBlock.gif   
if (this.textBox1.Text != null)
ExpandedSubBlockStart.gifContractedSubBlock.gif   
dot.gif{
InBlock.gif    
try
ExpandedSubBlockStart.gifContractedSubBlock.gif    
dot.gif{
InBlock.gif     bm.Save(memStream, System.Drawing.Imaging.ImageFormat.Jpeg);
InBlock.gif     
int size = (int)memStream.Length;
ExpandedSubBlockStart.gifContractedSubBlock.gif     
/**////处理上载的文件流信息。
InBlock.gif     byte[] b = new byte[size];
InBlock.gif     b 
= memStream.GetBuffer();
InBlock.gif     memStream.Close();
InBlock.gif     pic.Service up 
= new getanddownpic.pic.Service();
InBlock.gif     
string r = up.UploadFile(b, this.textBox1.Text);
InBlock.gif     MessageBox.Show(r);
ExpandedSubBlockEnd.gif    }

InBlock.gif    
catch (Exception ex)
ExpandedSubBlockStart.gifContractedSubBlock.gif    
dot.gif{
InBlock.gif     MessageBox.Show(ex.Message);
ExpandedSubBlockEnd.gif    }

ExpandedSubBlockEnd.gif   }

InBlock.gif   
else
ExpandedSubBlockStart.gifContractedSubBlock.gif   
dot.gif{
InBlock.gif    MessageBox.Show(
"please input a file name");
ExpandedSubBlockEnd.gif   }

ExpandedBlockEnd.gif  }

None.gif
None.gif

转载于:https://www.cnblogs.com/dengsu888666/archive/2006/07/22/457168.html

评论
添加红包

请填写红包祝福语或标题

红包个数最小为10个

红包金额最低5元

当前余额3.43前往充值 >
需支付:10.00
成就一亿技术人!
领取后你会自动成为博主和红包主的粉丝 规则
hope_wisdom
发出的红包
实付
使用余额支付
点击重新获取
扫码支付
钱包余额 0

抵扣说明:

1.余额是钱包充值的虚拟货币,按照1:1的比例进行支付金额的抵扣。
2.余额无法直接购买下载,可以购买VIP、付费专栏及课程。

余额充值