Skip to content
Open
Changes from all commits
Commits
File filter

Filter by extension

Filter by extension

Conversations
Failed to load comments.
Loading
Jump to
Jump to file
Failed to load files.
Loading
Diff view
Diff view
54 changes: 29 additions & 25 deletions src/WebApiContrib.Formatting.Xlsx/XlsxMediaTypeFormatter.cs
Original file line number Diff line number Diff line change
Expand Up @@ -96,31 +96,35 @@ public override void SetDefaultContentHeaders(Type type, HttpContentHeaders head
rawUri = rawUri.Substring(0, queryStringIndex);
}

string fileName;

// Look for ExcelDocumentAttribute on class.
var itemType = util.GetEnumerableItemType(type);
var excelDocumentAttribute = util.GetAttribute<ExcelDocumentAttribute>(itemType ?? type);

if (excelDocumentAttribute != null && !string.IsNullOrEmpty(excelDocumentAttribute.FileName))
{
// If attribute exists with file name defined, use that.
fileName = excelDocumentAttribute.FileName;
}
else
{
// Otherwise, use either the URL file name component or just "data".
fileName = System.Web.VirtualPathUtility.GetFileName(rawUri) ?? "data";
}

// Add XLSX extension if not present.
if (!fileName.EndsWith("xlsx", StringComparison.CurrentCultureIgnoreCase)) fileName += ".xlsx";

// Set content disposition to use this file name.
headers.ContentDisposition = new ContentDispositionHeaderValue("inline")
{ FileName = fileName };

base.SetDefaultContentHeaders(type, headers, mediaType);
if (headers.ContentDisposition == null)
{

string fileName;

// Look for ExcelDocumentAttribute on class.
var itemType = util.GetEnumerableItemType(type);
var excelDocumentAttribute = util.GetAttribute<ExcelDocumentAttribute>(itemType ?? type);

if (excelDocumentAttribute != null && !string.IsNullOrEmpty(excelDocumentAttribute.FileName))
{
// If attribute exists with file name defined, use that.
fileName = excelDocumentAttribute.FileName;
}
else
{
// Otherwise, use either the URL file name component or just "data".
fileName = System.Web.VirtualPathUtility.GetFileName(rawUri) ?? "data";
}

// Add XLSX extension if not present.
if (!fileName.EndsWith("xlsx", StringComparison.CurrentCultureIgnoreCase)) fileName += ".xlsx";

// Set content disposition to use this file name.
headers.ContentDisposition = new ContentDispositionHeaderValue("inline")
{FileName = fileName};
}

base.SetDefaultContentHeaders(type, headers, mediaType);
}

[SecurityPermission(SecurityAction.Demand, SerializationFormatter = true)]
Expand Down