using System;
using System.Collections.Generic;
using System.Linq;
using System.ServiceModel;
using System.ServiceModel.Activation;
using System.ServiceModel.Web;
using System.Text;
using System.IO;
using System.Web;

namespace WcfRestService5
{
    [ServiceContract]
    [AspNetCompatibilityRequirements(RequirementsMode = AspNetCompatibilityRequirementsMode.Allowed)]
    [ServiceBehavior(InstanceContextMode = InstanceContextMode.PerCall)]
    public class FileService
    {

        [WebGet(UriTemplate = "")]
        public string ShowTest()
        {
            return "只是测试一下";
        }

        /// <summary>
        /// 显示图片
        /// </summary>
        /// <param name="image">图片名</param>
        /// <returns>图片字节流</returns>
        [WebGet(UriTemplate = "Show/{image}")]
        public Stream ShowImage(string image)
        {
            string folder = System.Web.Hosting.HostingEnvironment.MapPath("~/images");
            WebOperationContext.Current.OutgoingResponse.ContentType = "image/jpeg";
            var file = Path.Combine(folder, image);
            return File.OpenRead(file);
        }

        /// <summary>
        /// 下载文件，以HTTP响应方式写回
        /// </summary>
        /// <param name="filename">文件名</param>
        [WebGet(UriTemplate = "DownLoad/{filename}")]
        public void  GetFile(string filename)
        {
            string folder = System.Web.Hosting.HostingEnvironment.MapPath("~/files");
            var FullName = Path.Combine(folder, filename);
            if (File.Exists(FullName))
            {
                FileInfo DownLoadFile = new FileInfo(FullName);
                HttpContext.Current.Response.Clear();
                HttpContext.Current.Response.ClearHeaders();
                HttpContext.Current.Response.Buffer = false;
                HttpContext.Current.Response.ContentType = "application/octer-stream";
                HttpContext.Current.Response.AppendHeader("Content-Disposition", "attachment:filename=" +
                    HttpUtility.UrlEncode(DownLoadFile.FullName, System.Text.Encoding.UTF8));
                HttpContext.Current.Response.AppendHeader("Content-Length", DownLoadFile.Length.ToString());
                HttpContext.Current.Response.WriteFile(DownLoadFile.FullName);
                HttpContext.Current.Response.Flush();
                HttpContext.Current.Response.End();
            }

        }

       
        /// <summary>
        /// 上传文件,这个地方用form表单提交文件测试是成功了的,file是要保存的文件名
        /// </summary>
        /// <param name="stream">上传文件字节流</param>
        /// <param name="file">要保存的文件名</param>
        /// <returns></returns>
        [WebInvoke(UriTemplate = "Add/{file}", Method = "POST")]
        public bool AddFile(Stream stream, string file)
        {
            if (!string.IsNullOrEmpty(file))
            {
                string folder = System.Web.Hosting.HostingEnvironment.MapPath("~/Files");
                if (!Directory.Exists(folder))
                {
                    Directory.CreateDirectory(folder);
                }
                string path = Path.Combine(folder, file);
                using (FileStream fs = new FileStream(path, FileMode.Create, FileAccess.Write))
                {
                    long ByteLength = WebOperationContext.Current.IncomingRequest.ContentLength;
                    byte[] fileContent = new byte[ByteLength];
                    stream.Read(fileContent, 0, fileContent.Length);
                    fs.Write(fileContent, 0, fileContent.Length);
                    fs.Flush();
                    return true;
                }
            }
            return false;
        }
    }
}