Wednesday

c# return zip stream from api

  
        public Stream GetDataFile(string locationId)
        {
            int cid;
            DateTime tran = DateTime.Now.Date;

            Check(string.IsNullOrWhiteSpace(locationId), "locationId field is missing.");
            Check(!int.TryParse(locationId, out cid), "locationId must be numeric.");


            DataFileRepository repository = new DataFileRepository();
            /*returning stream*/
            var resp = WebOperationContext.Current.OutgoingResponse;
            resp.ContentType = "application/octet-stream";
            resp.Headers.Add("Content-Disposition", "attachment; filename=\"myfile.zip\"");

            return ZipStream(repository.GetCSVStream(cid));
        }

        /// <summary>
        ///  Check condition and generate exception if condition met.
        /// </summary>
        /// <param name="condition"></param>
        /// <param name="exp"></param>
        public void Check(bool condition, string exp)
        {
            if (condition)
            {
                AssertionException up = new AssertionException(exp);
                throw up;
            }
        }

        /// <summary>
        ///  Method for zipping streams 
        /// </summary>
        /// <param name="stream"></param>
        /// <returns></returns>
        public Stream ZipStream(Stream stream)
        {
            stream.Position = 0;
            var ar = new ZipArchive();
            ar.AddItem("myfile.csv", stream, true, FileAttributes.Normal);

            var zip = new TempFileStream();
            ar.Save(zip, false);
            zip.Position = 0;
            return zip;
        }
interface declaration in API webservice is very simple:
      
  [OperationContract]
        [WebGet(UriTemplate = "menu/datafile/{locationId}")]
        Stream GetDataFile(string locationId);
   

IDataReader into csv

        
        /// <summary>
        ///  Method for converting reader into CSV
        /// </summary>
        /// <param name="dataReader"></param>
        /// <param name="includeHeaderAsFirstRow"></param>
        /// <param name="separator"></param>
        /// <returns></returns>
        public  Stream ToCSV(IDataReader dataReader, bool includeHeaderAsFirstRow, string separator)
        {
            Stream csv = new TempFileStream();
            StreamWriter csvRows = new StreamWriter(csv);
            StringBuilder sb = null;

            if (includeHeaderAsFirstRow)
            {
                sb = new StringBuilder();
                for (int index = 0; index < dataReader.FieldCount; index++)
                {
                    if (dataReader.GetName(index) != null)
                        sb.Append(dataReader.GetName(index));

                    if (index < dataReader.FieldCount - 1)
                        sb.Append(separator);
                }
                csvRows.WriteLine(sb.ToString());
            }

            while (dataReader.Read())
            {
                sb = new StringBuilder();
                for (int index = 0; index < dataReader.FieldCount - 1; index++)
                {
                    if (!dataReader.IsDBNull(index))
                    {
                        string value = dataReader.GetValue(index).ToString();
                        if (dataReader.GetFieldType(index) == typeof(String))
                        {
                            //If double quotes are used in value, ensure each are replaced but 2.
                            if (value.IndexOf("\"") >= 0)
                                value = value.Replace("\"", "\"\"");

                            //If separtor are is in value, ensure it is put in double quotes.
                            if (value.IndexOf(separator) >= 0)
                                value = "\"" + value + "\"";
                        }
                        sb.Append(value);
                    }

                    if (index < dataReader.FieldCount - 1)
                        sb.Append(separator);
                }

                if (!dataReader.IsDBNull(dataReader.FieldCount - 1))
                    sb.Append(dataReader.GetValue(dataReader.FieldCount - 1).ToString().Replace(separator, " "));

                csvRows.WriteLine(sb.ToString());
            }

            csvRows.Flush();
            dataReader.Close();
            return csv;
        }
 

imagemagic add text to image

rem different types of text annotations on existing images rem cyan yellow orange gold rem -gravity SouthWest rem draw text and anno...