50 lines
1.4 KiB
C#
50 lines
1.4 KiB
C#
using Microsoft.SharePoint;
|
|
using System;
|
|
using System.Globalization;
|
|
using System.IO;
|
|
using System.Web;
|
|
namespace SPSolutions.SharePoint.Web
|
|
{
|
|
public static class HttpUtil
|
|
{
|
|
public static void TransmitSPBinaryFile(string url)
|
|
{
|
|
SPWeb sPWeb = new SPSite(url).OpenWeb();
|
|
SPFile sPFile = sPWeb.GetFileOrFolderObject(url) as SPFile;
|
|
if (sPFile == null)
|
|
{
|
|
throw new NullReferenceException(url);
|
|
}
|
|
string sPFileContentType = HttpUtil.GetSPFileContentType(sPFile);
|
|
HttpResponse response = HttpContext.Current.Response;
|
|
response.ClearContent();
|
|
response.ClearHeaders();
|
|
response.AddHeader("Content-Disposition", "inline; filename=" + sPFile.Name);
|
|
response.ContentType = sPFileContentType;
|
|
using (Stream stream = sPFile.OpenBinaryStream())
|
|
{
|
|
byte[] buffer = new byte[sPFile.Length];
|
|
stream.Read(buffer, 0, (int)sPFile.Length);
|
|
response.BinaryWrite(buffer);
|
|
}
|
|
response.End();
|
|
}
|
|
public static string GetSPFileContentType(SPFile file)
|
|
{
|
|
if (file == null || string.IsNullOrEmpty(file.Name))
|
|
{
|
|
return "application/octet-stream";
|
|
}
|
|
if (string.IsNullOrEmpty(file.Name))
|
|
{
|
|
throw new ArgumentException("The file name is empty. Could not determine content type.");
|
|
}
|
|
if (file.Name.EndsWith("zip", true, CultureInfo.InvariantCulture))
|
|
{
|
|
return "application/zip";
|
|
}
|
|
return "application/octet-stream";
|
|
}
|
|
}
|
|
}
|