日韩性视频-久久久蜜桃-www中文字幕-在线中文字幕av-亚洲欧美一区二区三区四区-撸久久-香蕉视频一区-久久无码精品丰满人妻-国产高潮av-激情福利社-日韩av网址大全-国产精品久久999-日本五十路在线-性欧美在线-久久99精品波多结衣一区-男女午夜免费视频-黑人极品ⅴideos精品欧美棵-人人妻人人澡人人爽精品欧美一区-日韩一区在线看-欧美a级在线免费观看

歡迎訪問 生活随笔!

生活随笔

當前位置: 首頁 > 编程资源 > 编程问答 >内容正文

编程问答

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

發布時間:2023/12/10 编程问答 46 豆豆
生活随笔 收集整理的這篇文章主要介紹了 通过Web Services上传和下载图片文件 小編覺得挺不錯的,現在分享給大家,幫大家做個參考.

???? 通過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");
???}

??}


轉載于:https://www.cnblogs.com/dengsu888666/archive/2006/07/22/457168.html

總結

以上是生活随笔為你收集整理的通过Web Services上传和下载图片文件的全部內容,希望文章能夠幫你解決所遇到的問題。

如果覺得生活随笔網站內容還不錯,歡迎將生活随笔推薦給好友。