コード例 #1
0
ファイル: OutputZip.cs プロジェクト: KhaledSMQ/WebTools
        /// <summary>
        /// Zip files and Download
        /// </summary>
        /// <param name="response">Current HttpResponse</param>
        /// <param name="zipFileToCreate">Zip save path</param>
        /// <param name="files">zipfile</param>
        public static void ResponseZip(System.Web.HttpResponse response, string zipFileToCreate, List <string> files)
        {
            try
            {
                using (ZipFile zip = new ZipFile())
                {
                    foreach (string filename in files)
                    {
                        ZipEntry e = zip.AddFile(filename, string.Empty);
                        e.Comment = "Added by  VAU";
                    }

                    zip.Comment =
                        string.Format(
                            "This zip archive was created by the CreateZip example application on machine '{0}'",
                            System.Net.Dns.GetHostName());

                    zip.Save(zipFileToCreate);
                }

                response.Clear();
                response.AppendHeader("Content-Disposition", "attachment; filename=VAUFiles.zip");
                response.ContentType = "application/x-zip-compressed";
                response.WriteFile(zipFileToCreate);
                if (response.IsClientConnected)
                {
                    response.Flush();
                    response.Close();
                }
            }
            catch (System.Exception ex)
            {
                throw ex;
            }
        }
コード例 #2
0
ファイル: OutputExcel.cs プロジェクト: KhaledSMQ/WebTools
        public static void ResponseExcel <T>(System.Web.HttpResponse response, List <T> items)
        {
            try
            {
                string attachment = "attachment; filename=vauExcel.xls";
                response.ClearContent();
                response.AddHeader("content-disposition", attachment);
                response.ContentType = "application/vnd.ms-excel";
                string tab = string.Empty;

                // Get all the properties
                PropertyInfo[] props = typeof(T).GetProperties(BindingFlags.Public | BindingFlags.Instance);
                foreach (PropertyInfo prop in props)
                {
                    response.Write(tab + prop.Name);
                    tab = "\t";
                }

                response.Write("\n");
                foreach (T item in items)
                {
                    var values = new object[props.Length];
                    for (int i = 0; i < props.Length; i++)
                    {
                        values[i] = props[i].GetValue(item, null);
                        if (values[i] != null)
                        {
                            response.Write(values[i].ToString().Trim() + "\t");
                        }
                        else
                        {
                            response.Write("\t");
                        }
                    }

                    response.Write("\n");
                }

                response.Flush();
                response.Close();
            }
            catch (Exception ex)
            {
                throw ex;
            }
        }
コード例 #3
0
        public JObject DownloadFile(string fileName)
        {
            JObject obj = new JObject();

            try
            {
                System.Web.HttpRequest httpRequest = System.Web.HttpContext.Current.Request;
                string filePath = System.Web.HttpContext.Current.Server.MapPath("~/ApiUploadFile/");
                filePath = Path.Combine(System.Web.HttpContext.Current.Server.MapPath("~/ApiUploadFile/"), fileName);
                if (File.Exists(filePath))
                {
                    System.Web.HttpResponse response = System.Web.HttpContext.Current.Response;
                    response.Clear();
                    response.ClearHeaders();
                    response.ClearContent();
                    response.Buffer = true;
                    response.AddHeader("content-disposition", string.Format("attachment; FileName={0}", fileName));
                    response.Charset         = "utf-8";
                    response.ContentEncoding = System.Text.Encoding.GetEncoding("GB2312");
                    response.ContentType     = System.Web.MimeMapping.GetMimeMapping(fileName);
                    response.WriteFile(filePath);
                    response.Flush();
                    response.Close();
                    obj.Add("success", true);
                    obj.Add("message", "");
                }
                else
                {
                    obj.Add("success", false);
                    obj.Add("message", "文件不存在!");
                }
            }
            catch (Exception ex)
            {
                obj.Add("success", false);
                obj.Add("message", ex.Message);
            }
            return(obj);
        }