public static string GetHeaderValue(string fileName, DispositionResultType dispositionResultType) { try { // first, try using the .NET built-in generator ContentDisposition disposition = new ContentDisposition() { FileName = fileName, Inline = dispositionResultType == DispositionResultType.Inline ? true : false }; return(disposition.ToString()); } catch (FormatException) { // otherwise, fall back to RFC 2231 extensions generator return(CreateRfc2231HeaderValue(fileName, dispositionResultType)); } }
private static string CreateRfc2231HeaderValue(string filename, DispositionResultType dispositionResultType) { StringBuilder builder = new StringBuilder(string.Format("{0}; filename*=UTF-8''", dispositionResultType.ToString().ToLower())); byte[] filenameBytes = Encoding.UTF8.GetBytes(filename); foreach (byte b in filenameBytes) { if (IsByteValidHeaderValueCharacter(b)) { builder.Append((char)b); } else { AddByteToStringBuilder(b, builder); } } return(builder.ToString()); }