Example #1
0
        public HttpResponseMessage DownloadTemplate(int id)
        {
            //下载示例
            if (id == 0)
            {
                var examplePath = string.Format("{0}{1}{2}", RootPath, "statics\\tmplExample\\", "example.zip");
                #region --- 读取文件并返回 ---
                byte[] byteArr             = File.ReadAllBytes(examplePath);
                HttpResponseMessage result = new HttpResponseMessage(HttpStatusCode.OK);
                result.Content = new ByteArrayContent(byteArr);
                result.Content.Headers.ContentType = new MediaTypeHeaderValue("application/octet-stream");
                ContentDispositionHeaderValue contentDisposition = new ContentDispositionHeaderValue("attachment");
                contentDisposition.FileName = "example.zip";
                result.Content.Headers.ContentDisposition = contentDisposition;
                #endregion
                return(result);
            }

            var tmp  = _templateService.GetTemplateById(id);
            var tmpl = tmp.Data as TemplateInfo;
            if (tmpl != null && !string.IsNullOrWhiteSpace(tmpl.StoredName))
            {
                //string fileName = string.Format("{0}{1}", this.RootPath, tmpl.StoredName);
                //fileName = @"C:\Users\Administrator\Desktop\version\temp\A123.jpg";
                var fileName = tmpl.StoredName;

                if (Directory.Exists(fileName))
                {
                    var plist = new List <string>();
                    plist.Add(string.Format(fileName));
                    //plist.Add(string.Format("{0}{1}", fileName, "\\base.css"));
                    //plist.Add(string.Format("{0}{1}", fileName, "\\common.css"));
                    var zipPath = string.Format("{0}{1}{2}", RootPath, "statics\\tmpl\\", "template.zip");
                    ZipHelper.CompressMulti(plist, zipPath, true);

                    #region --- 读取文件并返回 ---
                    byte[] byteArr             = File.ReadAllBytes(zipPath);
                    HttpResponseMessage result = new HttpResponseMessage(HttpStatusCode.OK);
                    result.Content = new ByteArrayContent(byteArr);
                    result.Content.Headers.ContentType = new MediaTypeHeaderValue("application/octet-stream");
                    ContentDispositionHeaderValue contentDisposition = new ContentDispositionHeaderValue("attachment");
                    contentDisposition.FileName = string.Format("{0}.zip", tmpl.TmplTitle);
                    result.Content.Headers.ContentDisposition = contentDisposition;
                    #endregion
                    return(result);
                }
            }
            return(Request.CreateResponse(HttpStatusCode.NotFound));
        }