惯性聚合 高效追踪和阅读你感兴趣的博客、新闻、科技资讯
阅读原文 在惯性聚合中打开

推荐订阅源

Blog — PlanetScale
Blog — PlanetScale
Jina AI
Jina AI
C
Check Point Blog
V
V2EX
H
Help Net Security
Microsoft Azure Blog
Microsoft Azure Blog
P
Proofpoint News Feed
A
About on SuperTechFans
D
DataBreaches.Net
腾讯CDC
Cyber Security Advisories - MS-ISAC
Cyber Security Advisories - MS-ISAC
IT之家
IT之家
WordPress大学
WordPress大学
人人都是产品经理
人人都是产品经理
T
The Blog of Author Tim Ferriss
Recent Announcements
Recent Announcements
Google DeepMind News
Google DeepMind News
云风的 BLOG
云风的 BLOG
MongoDB | Blog
MongoDB | Blog
J
Java Code Geeks
博客园_首页
T
Tailwind CSS Blog
M
MIT News - Artificial intelligence
OSCHINA 社区最新新闻
OSCHINA 社区最新新闻

博客园 - 大白鲨

"Maximum length exceeded"错误的解决办法 - 大白鲨 javascript的数组使用方法 - 大白鲨 - 博客园 asp.net操作Excel拒绝访问的解决 利用javascript判断浏览器是否已经安装ActiveX控件和是否禁止运行ActiveX控件 IIS错误Server Application Error(http 500 错误)解决办法 SQL Server的SQL技巧 ASP.NET 2.0 实现多文件上传 网络MSDTC配置方法 visual studio 2005包加载失败的解决办法 Office Excel 2003找不到安装文件sku011.cab的解决办法 delphi连接DCOM的安全配置 Atlas中对DataTable操作的脚本 去掉asp.net2.0的树控件刷新功能 IIS 5.0错误解决集锦 访问IIS时返回的状态代码解释参考 使用UDPClient 编写聊天程序 Windows安全配置技术 为IIS配置安全的发布方法 SQL Server中各个系统表的作用
通过Web Services上传和下载图片文件
大白鲨 · 2006-07-22 · via 博客园 - 大白鲨

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

    随着Internet技术的发展和跨平台需求的日益增加,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");
   }

  }