private string GetHeader(Field field) { if (field.type == FieldType.End) return string.Format("--{0}--", _boundary); if (field.type == FieldType.Spacer) return "\r\n"; var str = new StringBuilder(); str.AppendFormat("--{0}", _boundary); str.Append(NewLine); str.AppendFormat(@"Content-Disposition: form-data; name=""{0}""", field.name); if (field.type == FieldType.File) str.AppendFormat(@"; filename=""{0}""", Path.GetFileName(field.fileName)); str.Append(NewLine); if (field.type == FieldType.File) { if (string.IsNullOrWhiteSpace(field.contentType)) field.contentType = "application/octet-stream"; } str.AppendFormat(@"Content-Type: {0}", field.contentType); str.Append(NewLine); str.Append(NewLine); return str.ToString(); }
private void PreProcessField(Field field) { if (field.type == FieldType.File) { if (field.stream == null) field.stream = File.OpenRead(field.fileName); } else if (field.type == FieldType.Text || field.type == FieldType.End || field.type == FieldType.Spacer) { if (field.stream == null) field.stream = new MemoryStream(Encoding.UTF8.GetBytes(field.value)); } if (field.header == null) { field.header = GetHeader(field); field.headerBytes = Encoding.ASCII.GetBytes(field.header); } }
public object Clone() { var field = new Field(); field.name = name; field.type = type; field.fileName = fileName; field.contentType = contentType; field.value = value; return field; }