下载文件
如果下载其它类别的文件:

SPSecurity.RunWithElevatedPrivileges(delegate()
{
using (SPSite currentSite = new SPSite(SPContext.Current.Site.ID))
{
using (SPWeb web = currentSite.OpenWeb(SPContext.Current.Web.ID))
{
string filename = Request.QueryString["FileName"].ToString();
string ItemID = Request.QueryString["ID"].ToString();
web.AllowUnsafeUpdates = true;
string strContentType = "";
SPList list = web.Lists["TestList"];
SPFolder folder = web.Folders["Lists"].SubFolders["TestList"].SubFolders["Attachments"].SubFolders[ItemID.ToString()];
string url = folder.Url + "/" + filename.ToString();
SPFile tempFile = web.GetFile(url);
string[] file = filename.Split('.');
int a = file.Length;
//Get the extension of File.
byte[] obj = (byte[])tempFile.OpenBinary();
// Get the extension of File to determine the file type
string casestring = "";
casestring = file[a - 1].ToString();
switch (casestring)
{
case "txt":
strContentType = "text/plain";
break;
case "htm":
strContentType = "text/html";
break;
case "html":
strContentType = "text/html";
break;
case "rtf":
strContentType = "text/richtext";
break;
case "jpg":
strContentType = "image/jpeg";
break;
case "jpeg":
strContentType = "image/jpeg";
break;
case "gif":
strContentType = "image/gif";
break;
case "bmp":
strContentType = "image/bmp";
break;
case "mpg":
strContentType = "video/mpeg";
break;
case "mpeg":
strContentType = "video/mpeg";
break;
case "avi":
strContentType = "video/avi";
break;
case "pdf":
strContentType = "application/pdf";
break;
case "doc":
strContentType = "application/msword";
break;
case "dot":
strContentType = "application/msword";
break;
case "csv":
strContentType = "application/vnd.msexcel";
break;
case ".xls":
strContentType = "application/vnd.msexcel";
break;
case ".xlt":
strContentType = "application/vnd.msexcel";
break;
default:
strContentType = "application/octet-stream";
break;
}
Response.ClearContent();
Response.ClearHeaders();
Response.AppendHeader("Content-Disposition", "attachment; filename=" + filename.ToString());
Response.ContentType = strContentType;
//Check that the client is connected and has not closed the connection after the request
if (Response.IsClientConnected)
{
Response.BinaryWrite(obj);
}
Response.Flush();
Response.Close();
}
}
});
}
catch (Exception ex)
{
}
{
using (SPSite currentSite = new SPSite(SPContext.Current.Site.ID))
{
using (SPWeb web = currentSite.OpenWeb(SPContext.Current.Web.ID))
{
string filename = Request.QueryString["FileName"].ToString();
string ItemID = Request.QueryString["ID"].ToString();
web.AllowUnsafeUpdates = true;
string strContentType = "";
SPList list = web.Lists["TestList"];
SPFolder folder = web.Folders["Lists"].SubFolders["TestList"].SubFolders["Attachments"].SubFolders[ItemID.ToString()];
string url = folder.Url + "/" + filename.ToString();
SPFile tempFile = web.GetFile(url);
string[] file = filename.Split('.');
int a = file.Length;
//Get the extension of File.
byte[] obj = (byte[])tempFile.OpenBinary();
// Get the extension of File to determine the file type
string casestring = "";
casestring = file[a - 1].ToString();
switch (casestring)
{
case "txt":
strContentType = "text/plain";
break;
case "htm":
strContentType = "text/html";
break;
case "html":
strContentType = "text/html";
break;
case "rtf":
strContentType = "text/richtext";
break;
case "jpg":
strContentType = "image/jpeg";
break;
case "jpeg":
strContentType = "image/jpeg";
break;
case "gif":
strContentType = "image/gif";
break;
case "bmp":
strContentType = "image/bmp";
break;
case "mpg":
strContentType = "video/mpeg";
break;
case "mpeg":
strContentType = "video/mpeg";
break;
case "avi":
strContentType = "video/avi";
break;
case "pdf":
strContentType = "application/pdf";
break;
case "doc":
strContentType = "application/msword";
break;
case "dot":
strContentType = "application/msword";
break;
case "csv":
strContentType = "application/vnd.msexcel";
break;
case ".xls":
strContentType = "application/vnd.msexcel";
break;
case ".xlt":
strContentType = "application/vnd.msexcel";
break;
default:
strContentType = "application/octet-stream";
break;
}
Response.ClearContent();
Response.ClearHeaders();
Response.AppendHeader("Content-Disposition", "attachment; filename=" + filename.ToString());
Response.ContentType = strContentType;
//Check that the client is connected and has not closed the connection after the request
if (Response.IsClientConnected)
{
Response.BinaryWrite(obj);
}
Response.Flush();
Response.Close();
}
}
});
}
catch (Exception ex)
{
}