[轉貼] 利用Web Service上傳檔案,整合WebFrom與WinForm

 

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

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

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

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

Web Service

Fileupload.asmx

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

Fileupload.cs

using System;
using System.Web;
using System.Collections;
using System.Web.Services;
using System.Web.Services.Protocols;
using System.IO;


/// <summary>
/// Fileupload 的摘要描述
/// </summary>
[WebService(Namespace = "http://tempuri.org/")]
[WebServiceBinding(ConformsTo = WsiProfiles.BasicProfile1_1)]
public class Fileupload : System.Web.Services.WebService
{
    public Fileupload()
    {

    }

    [WebMethod]
    public string UploadFile(byte[] fs, string fileName)
    {
        try
        {
            MemoryStream memoryStream = new MemoryStream(fs);
            FileStream fileStream = new FileStream(Server.MapPath(@"~\upload\" + fileName), FileMode.Create);
            memoryStream.WriteTo(fileStream);
            memoryStream.Close();
            fileStream.Close();
            fileStream = null;
            memoryStream = null;
            return "上傳成功";
        }

        catch (Exception ex)
        {
            return ex.Message;
        }
    }
}

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

WebForm

ServiceFileupload.aspx

<%@ Page Language="C#" AutoEventWireup="true" CodeFile="ServiceFileupload.aspx.cs" Inherits="ServiceFileupload" %>

<!DOCTYPE html PUBLIC "-//W3C//DTD XHTML 1.0 Transitional//EN" "http://www.w3.org/TR/xhtml1/DTD/xhtml1-transitional.dtd">

<html xmlns="http://www.w3.org/1999/xhtml" >
<head runat="server">
    <title>ServieFileupload</title>
</head>
<body>
    <form id="form1" runat="server">
    <div>
        <asp:FileUpload ID="FileUpload1" runat="server" />
        <asp:Button ID="btnUpload" runat="server" OnClick="btnUpload_Click" Text="Upload" /></div>
    </form>
</body>
</html>

ServiceFileupload.aspx.cs

using System;
using System.Data;
using System.Configuration;
using System.Collections;
using System.Web;
using System.Web.Security;
using System.Web.UI;
using System.Web.UI.WebControls;
using System.Web.UI.WebControls.WebParts;
using System.Web.UI.HtmlControls;
using Service_Fileupload;

public partial class ServiceFileupload : System.Web.UI.Page
{
    protected void Page_Load(object sender, EventArgs e)
    {
       
    }

    protected void btnUpload_Click(object sender, EventArgs e)
    {
        if(this.FileUpload1.HasFile)
        {
            Response.Write(new Fileupload().UploadFile(this.FileUpload1.FileBytes, this.FileUpload1.FileName));
        }
    }
}

WinForm

ServiceFileupload.cs

using System;
using System.Collections.Generic;
using System.ComponentModel;
using System.Data;
using System.Drawing;
using System.Text;
using System.Windows.Forms;
using System.IO;
using WindowsApplication1.Service_Fileupload1;

namespace WindowsApplication1
{
    public partial class ServiceFileupload : Form
    {
        public ServiceFileupload()
        {
            InitializeComponent();
        }

        private void btnOpen_Click(object sender, EventArgs e)
        {
            OpenFileDialog f = new OpenFileDialog();
            f.ShowDialog(this);
            this.txbPath.Text = f.FileName;
        }

        private void btnUpload_Click(object sender, EventArgs e)
        {
            this.label1.Text = new Fileupload().UploadFile(File.ReadAllBytes(this.txbPath.Text), Path.GetFileName(this.txbPath.Text));
        }
    }
}


參考網址:

http://www.c-sharpcorner.com/UploadFile/subcom/003202008114211AM/0.aspx


留言

這個網誌中的熱門文章

TeknoParrot 模擬器介紹,俗稱《鸚鵡模擬器》

[轉貼] ASP.NET傳遞參數給Crystal Report做Export或Print的動作

Raspberry Pi 樹莓派 - Raspbian 忘記密碼簡單重設