从sharepoint 2007文档库下载文件的代码。请在此代码中更正我

时间:2011-07-26 06:11:29

标签: sharepoint sharepoint-2010 sharepoint-2007

我正在使用此代码在按钮点击下载文件。 代码工作完美但只存在一个问题,即代码仅在第一次完美执行时,首先是当我点击下载按钮文件时下载但是当我第二次点击下载按钮时没有任何反应。 因此,为了再次执行代码,我需要进行页面刷新(即F5),一旦刷新完成,然后再次代码执行完美但仅用于第一次单击 请在此代码中纠正我的错误,以便代码能够正常运行

try

{

int flag=0;

SPSite site = new SPSite(sharePointURL); 

SPWeb web = site.OpenWeb(); 

web.AllowUnsafeUpdates=true;

string strContentType=""; 
// docLib is the name of document library
SPFolder folder = web.GetFolder("docLib");

SPFileCollection files=folder.Files;
//"docLib" is name of document library and testFile.doc is the name of file
string url=sharePointURL+"/"+"docLib"+"/"+"testFile.doc"

SPFile tempFile = web.GetFile(url);

//Get the extension of File.

string []fext=this.filename[0].Split('.');

byte []obj=(byte[])tempFile.OpenBinary();


// Get the extension of File to determine the file type
string casestring="";
if(fext.Length>1)
{
 casestring= fext[fext.Length-1];
}
//set the content type of file according to extension
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[0]);

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)

{

} 

由于 Rushikesh

1 个答案:

答案 0 :(得分:2)

我知道发生了什么,只需将下面的代码放在你的aspx / ascx javascript头中。

<script type="text/javascript">

_spOriginalFormAction = document.forms[0].action;
_spSuppressFormOnSubmitWrapper = true;

</script>

在此之后,您将能够多次下载。