2011-07-26 5 views
0

私はこのコードを使用してボタンをクリックしてファイルをダウンロードしています。 コードは完璧に機能しますが、コードが完全に初めて実行されるという問題が1つのみ存在します。ダウンロードボタンをクリックするとファイルがダウンロードされますが、ダウンロードボタンを2回クリックすると何も起こりません。 コードをもう一度実行するには、ページリフレッシュ(F5)を実行する必要があります。リフレッシュが完了したら、コードは完全に実行されますが、最初のクリックのみです。 コードが完全に機能するようにこのコードで間違っていることを修正してください。Sharepoint 2007ドキュメントライブラリからファイルをダウンロードするコード。このコードで私を修正してください

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

+0

コードをより読みやすくするために、より良い応答を得ることができます - 空の行を削除し、正しくインデントします。 – Ryan

答えて

2

私はjavascriptの頭部あなたのASPX/ascxファイル内のコードの下に入れて、何が起こったか知っています。

<script type="text/javascript"> 

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

</script> 

この後、複数回ダウンロードすることができます。

+0

こんにちは。本当にありがとうございます。これは本当に助けと私の問題を解決した..ありがとう。もう1つの質問..問題が何であり、どのように解決したのか教えてください。おかげで – Rushikesh

+0

このページを見て、それは2番目のポストバックのための更新パネルと同じ理由です。 http://sharemypoint.wordpress.com/2007/11/11/webpart-and-ajax-second-postback-not-working-in-updatepanel/ –

+0

:)ありがとう – Rushikesh

関連する問題