<tr id="ohcdf"></tr>
    <ins id="ohcdf"><option id="ohcdf"></option></ins>

    <ins id="ohcdf"></ins>
  1. <tr id="ohcdf"></tr>

      <small id="ohcdf"></small><tr id="ohcdf"><small id="ohcdf"><delect id="ohcdf"></delect></small></tr><code id="ohcdf"></code>
        潤宇軟件
        首 頁 企業簡介 項目案例 軟件定制 行業軟件 解決方案 企業資訊 服務專區 客服中心
        業務介紹:西安軟件公司、軟件開發、軟件定制、軟件外包
        軟件 方案 文章
          潤宇軟件 >> 新聞資訊  >> 解決方案

        軟件開發中跨服務器通過Web Service來上傳文件

        發布時間:2022/8/15  瀏覽次數:次  字體【    】
        using System;
        using System.Data;
        using System.Web;
        using System.Collections;
        using System.Web.Services;
        using System.Web.Services.Protocols;
        using System.ComponentModel;
        using System.IO;
         
        namespace UpDownFile
        {
            /**/
            /// <summary>
            /// UpDownFile 的摘要說明
            /// </summary>
            [WebService(Namespace = "http://tempuri.org/")]
            [WebServiceBinding(ConformsTo = WsiProfiles.BasicProfile1_1)]
            [ToolboxItem(false)]
            public class UpDownFile : System.Web.Services.WebService
            {
                //上傳文件至WebService所在服務器的方法,這里為了操作方法,文件都保存在UpDownFile服務所在文件夾下的File目錄中
                [WebMethod]
                public bool Up(byte[] data, string filename)
                {
                    try
                    {
                        FileStream fs = File.Create(Server.MapPath("File/") + filename);
                        fs.Write(data, 0, data.Length);
                        fs.Close();
                        return true;
                    }
                    catch
                    {
                        return false;
                    }
                }
         
                //下載WebService所在服務器上的文件的方法
                [WebMethod]
                public byte[] Down(string filename)
                {
                    string filepath = Server.MapPath("File/") + filename;
                    if (File.Exists(filepath))
                    {
                        try
                        {
                            FileStream s = File.OpenRead(filepath);
                            return ConvertStreamToByteBuffer(s);
                        }
                        catch
                        {
                            return new byte[0];
                        }
                    }
                    else
                    {
                        return new byte[0];
                    }
                }
            }
        }
        上傳:
         

                    //FileUpload1是aspx頁面的一個FileUpload控件
                    UpDownFile.UpDownFile up = new UpDownFile.UpDownFile();
                    up.Up(ConvertStreamToByteBuffer(FileUpload1.PostedFile.InputStream),
                    FileUpload1.PostedFile.FileName.Substring(FileUpload1.PostedFile.FileName.LastIndexOf("\\") + 1));

        下載:
                    UpDownFile.UpDownFile down = new UpDownFile.UpDownFile();
                    byte[] file = down.Down(Request.QueryString["filename"].ToString()); //filename是要下載的文件路徑,可自行以其它方式獲取文件路徑
                    Response.BinaryWrite(file);

        以下是將文件流轉換成文件字節的函數(因為Stream類型的是不能直接通過WebService傳輸):

         protected byte[] ConvertStreamToByteBuffer(Stream s)
         {
            BinaryReader br = new BinaryReader(stream);
            byte[] fileBytes = br.ReadBytes((int)stream.Length);
            return fileBytes;
        }
          關閉本頁
        西部IT網合作伙伴 合作伙伴
        陜西省 | 榆林 | 延安 | 銅川 | 渭南 | 商洛 | 寶雞 | 漢中 | 安康 | 咸陽
        網站首頁 | 關于我們 | 售后服務 | 項目合同 | 查看留言 | 在線留言 | 客服中心
        © 版權所有:西安潤宇軟件科技有限公司 
        公司地址:西安市絲路國際創意夢工廠4號樓 聯系電話:029-87878512 手機:13468700578 聯系人:李先生
        Copyright ® 2009-2015 RunYusoft.com Inc. All Rights Reserved 
        技術支持:西安潤宇軟件科技有限公司  陜ICP備11000720號
        精品久久一区二区三区无码,久久亚洲AV成人无码国产精品,久久久久精品a毛片,亚洲精品无码九九九九