微信公众号搜"智元新知"关注
微信扫一扫可直接关注哦!

.net 写文件上传下载webservice

using System;
using System.Collections;
using System.ComponentModel;
using System.Data;
using System.Linq;
using System.Web;
using System.Web.Services;
using System.Web.Services.Protocols;
using System.Xml.Linq;
using System.IO;
using System.Text;

namespace updownFile
{
    /// <summary>
    /// Service1 的摘要说明
    /// </summary>
    [WebService(Namespace = "http://tempuri.org/")]
    [WebServiceBinding(ConformsTo = WsiProfiles.BasicProfile1_1)]
    [ToolBoxItem(false)]
    // 若要允许使用 ASP.NET AJAX 从脚本中调用此 Web 服务,请取消对下行的注释。
    // [System.Web.Script.Services.ScriptService]
    public class updownFile : System.Web.Services.WebService
    {
        private static string filesavepath = System.Configuration.ConfigurationManager.AppSettings.Get("filepath");
        /// <summary>
        /// 下载文件
        /// </summary>
        /// <param name="filename"></param>
        /// <returns></returns>
        [WebMethod]
        public bool DownFile(string filename,out byte[] bytes)
        {
            string filepath = filesavepath + filename;
            LogWrite(filepath);
            if (File.Exists(filepath))
            {
                try
                {
                    FileStream s = File.OpenRead(filepath);
                    bytes = ConvertStreamToByteBuffer(s);
                    s.Close();
                    return true;
                }
                catch
                {
                    bytes = new byte[0];
                    return false;
                }
            }
            else
            {
                bytes = new byte[0];
                return false;
            }
        }

        public byte[] ConvertStreamToByteBuffer(Stream s)
        {
            MemoryStream ms = new MemoryStream();
            int b;
            s.Seek(0,0);
            while ((b = s.ReadByte()) !=-1)
            {
                ms.WriteByte((byte)b);
            }
            return ms.ToArray();
        }

        [WebMethod]
        public bool UpFile(byte[] data,string filepath,string filename)
        {
            try
            {
                filepath = filesavepath + filepath;
                if (!Directory.Exists(filepath))
                {
                    Directory.CreateDirectory(filepath);
                }
                if (Directory.Exists(filepath))
                {
                    string fullname = filepath + filename;
                    if (File.Exists(fullname))
                    {
                        File.Delete(fullname);
                    }
                    FileStream fs = File.Create(fullname);
                    fs.Write(data,data.Length);
                    fs.Close();
                    return true;
                }
                else return false;
            }
            catch
            {
                return false;
            }
        }

        protected bool LogWrite( string content)
        {
            try
            {
                //读取
                string file = "C:\\11111.txt";
                StreamReader sr = new StreamReader(file,true);
                string s = sr.ReadLine();
                sr.Close();
                //写入 与源文件字符相加
                StreamWriter sw = new StreamWriter(file,true,Encoding.UTF8);
                sw.WriteLine(s + content);
                sw.Close();
                return true;
            }
            catch (Exception ex)
            {
                //Response.Write(ex.Message.ToString());
                return false;
            }
        }


    }
}

 

注意:一定要加 s.Seek(0,0);否则下载回来的文件数据会有丢失。

版权声明:本文内容由互联网用户自发贡献,该文观点与技术仅代表作者本人。本站仅提供信息存储空间服务,不拥有所有权,不承担相关法律责任。如发现本站有涉嫌侵权/违法违规的内容, 请发送邮件至 [email protected] 举报,一经查实,本站将立刻删除。

相关推荐