一篇簡單的Web Service上傳檔案,並且整合在WebFromWinForm使用者介面

首先要準備一支Web Service(上傳檔案用,WebForm或WinForm都會call這支)

接下來準備WebForm與WinForm的UI介面來上傳檔案

Web Service

Fileupload.asmx

  1. <%@ WebService Language="C#" CodeBehind="~/App_Code/Fileupload.cs" Class="Fileupload" %>  

Fileupload.cs

  1. using System;  
  2. using System.Web;  
  3. using System.Collections;  
  4. using System.Web.Services;  
  5. using System.Web.Services.Protocols;  
  6. using System.IO;  
  7.   
  8.   
  9. /// <summary>  
  10. /// Fileupload 的摘要描述  
  11. /// </summary>  
  12. [WebService(Namespace = "http://tempuri.org/")]  
  13. [WebServiceBinding(ConformsTo = WsiProfiles.BasicProfile1_1)]  
  14. public class Fileupload : System.Web.Services.WebService  
  15. {  
  16.     public Fileupload()  
  17.     {  
  18.   
  19.     }  
  20.   
  21.     [WebMethod]  
  22.     public string UploadFile(byte[] fs, string fileName)  
  23.     {  
  24.         try  
  25.         {  
  26.             MemoryStream memoryStream = new MemoryStream(fs);  
  27.             FileStream fileStream = new FileStream(Server.MapPath(@"~\upload\" + fileName), FileMode.Create);  
  28.             memoryStream.WriteTo(fileStream);  
  29.             memoryStream.Close();  
  30.             fileStream.Close();  
  31.             fileStream = null;  
  32.             memoryStream = null;  
  33.             return "上傳成功";  
  34.         }  
  35.   
  36.         catch (Exception ex)  
  37.         {  
  38.             return ex.Message;  
  39.         }  
  40.     }  
  41. }  

在WebForm或WinForm專案都要加入Web參考,如下圖所示

WebForm

ServiceFileupload.aspx

  1. <%@ Page Language="C#" AutoEventWireup="true" CodeFile="ServiceFileupload.aspx.cs" Inherits="ServiceFileupload" %>  
  2.   
  3. <!DOCTYPE html PUBLIC "-//W3C//DTD XHTML 1.0 Transitional//EN" "http://www.w3.org/TR/xhtml1/DTD/xhtml1-transitional.dtd">  
  4.   
  5. <html xmlns="http://www.w3.org/1999/xhtml" >  
  6. <head runat="server">  
  7.     <title>ServieFileupload</title>  
  8. </head>  
  9. <body>  
  10.     <form id="form1" runat="server">  
  11.     <div>  
  12.         <asp:FileUpload ID="FileUpload1" runat="server" />  
  13.         <asp:Button ID="btnUpload" runat="server" OnClick="btnUpload_Click" Text="Upload" /></div>  
  14.     </form>  
  15. </body>  
  16. </html>  

ServiceFileupload.aspx.cs

  1. using System;  
  2. using System.Data;  
  3. using System.Configuration;  
  4. using System.Collections;  
  5. using System.Web;  
  6. using System.Web.Security;  
  7. using System.Web.UI;  
  8. using System.Web.UI.WebControls;  
  9. using System.Web.UI.WebControls.WebParts;  
  10. using System.Web.UI.HtmlControls;  
  11. using Service_Fileupload;  
  12.   
  13. public partial class ServiceFileupload : System.Web.UI.Page  
  14. {  
  15.     protected void Page_Load(object sender, EventArgs e)  
  16.     {  
  17.           
  18.     }  
  19.   
  20.     protected void btnUpload_Click(object sender, EventArgs e)  
  21.     {  
  22.         if(this.FileUpload1.HasFile)  
  23.         {  
  24.             Response.Write(new Fileupload().UploadFile(this.FileUpload1.FileBytes, this.FileUpload1.FileName));  
  25.         }  
  26.     }  
  27. }  

WinForm

ServiceFileupload.cs

  1. using System;  
  2. using System.Collections.Generic;  
  3. using System.ComponentModel;  
  4. using System.Data;  
  5. using System.Drawing;  
  6. using System.Text;  
  7. using System.Windows.Forms;  
  8. using System.IO;  
  9. using WindowsApplication1.Service_Fileupload1;  
  10.   
  11. namespace WindowsApplication1  
  12. {  
  13.     public partial class ServiceFileupload : Form  
  14.     {  
  15.         public ServiceFileupload()  
  16.         {  
  17.             InitializeComponent();  
  18.         }  
  19.   
  20.         private void btnOpen_Click(object sender, EventArgs e)  
  21.         {  
  22.             OpenFileDialog f = new OpenFileDialog();  
  23.             f.ShowDialog(this);  
  24.             this.txbPath.Text = f.FileName;  
  25.         }  
  26.   
  27.         private void btnUpload_Click(object sender, EventArgs e)  
  28.         {  
  29.             this.label1.Text = new Fileupload().UploadFile(File.ReadAllBytes(this.txbPath.Text), Path.GetFileName(this.txbPath.Text));  
  30.         }  
  31.     }  
  32. }  


參考網址:

http://www.dotblogs.com.tw/puma/archive/2009/01/06/6660.aspx

Posted by romeogi1023 at 痞客邦 PIXNET Comments(0) Trackback(0) Hits(73)