2011-01-13 26 views
10

「あなたのファイルがダウンロードされている間お待ちください」と表示されているポップアップウィンドウがあります。このポップアップでは、以下のコードを実行してファイルのダウンロードを開始します。ファイルのダウンロードが完了したら、どうすればポップアップウィンドウを閉じることができますか?ファイルのダウンロードが完了したことを検出するには何らかの方法が必要です。このポップアップを閉じるにはself.close()を呼び出すことができます。ASP.NETでファイルのダウンロードが完了したときはどうすれば検出できますか?

System.Web.HttpContext.Current.Response.ClearContent(); 
System.Web.HttpContext.Current.Response.Clear(); 
System.Web.HttpContext.Current.Response.ClearHeaders(); 
System.Web.HttpContext.Current.Response.ContentType = fileObject.ContentType; 
System.Web.HttpContext.Current.Response.AppendHeader("Content-Disposition", string.Concat("attachment; filename=", fileObject.FileName)); 
System.Web.HttpContext.Current.Response.WriteFile(fileObject.FilePath); 
Response.Flush(); 
Response.End(); 
+7

短い答え...あなたがすることはできません! –

+0

ダウンロードはクライアント側、ASP.NETはサーバー側です。 –

+1

できません。なぜダウンロード待ちのユーザーインターフェイスがロックアップするのですか?ダウンロードしたファイルに依存しないように、そのような待ち時間なく、確実に正しいページの操作を続けることができます。ウェブはそのようには機能しません、なぜあなたはしたいですか? – spender

答えて

0

いくつかのハッキングはそれがバッファの最後のピースが送られたときに知っているかHttpResponse.IsClientConnectedプロパティをチェックする必要の周りにあります。

0

JavaScriptを使用して問題を処理していますが、問題が発生する場合もあります。

  • 「...ダウンロードしているファイル」 メッセージで、 なく、ポップアップボックスを隠しDIV要素を作成します。
  • ます。また、そうした後 ダウンロードメッセージのdiv要素を非表示にするには、タイマーを設定することができ
  • ..もう一度DIV を隠し、 フォーム上の他の要素がクリックされると、ダウンロードが
  • を開始 ときのdivを表示時間の 量...

私は、別の要素をユーザーがクリック後、彼女を理解どちらか、既にダウンロードが行われている知っている、または彼女が他の何かをする準備ができているので、メッセージは無関係になり、離れて行くことができます....

2

アイデア:

あなたが応答ストリームにチャンクでチャンクを書き込むことによって、サーバー側のコードの中で自分自身をダウンロードし、ファイルを扱う場合は、ファイルが完成しダウンロードを持っていたとき、あなたは知っていますよ。 FileStreamをレスポンスストリームに接続し、チャンクでチャンクを送信し、完了後にリダイレクトするだけです。これはあなたのポップアップウィンドウの内側にすることができます。

Response.ContentType = "application/octet-stream"; 
Response.AppendHeader("content-disposition", "attachment; filename=bob.mp3"); 
Response.AppendHeader("content-length", "123456789"); 

レスポンスストリームに書き出すときは、必ずResponse.IsClientConnectedをチェックしてください。

0

これを行う方法は、ファイルがフラッシュされたことを示す応答をAJAXポーリングを介してサーバーに呼び出すポップアップです。

例:ファイルを送信する直前に、sessionID + FileNameをDBまたはセッションに保存してください。クライアントで

は、あなたのポップアップには、AJAXを介してウェブサービスをポーリング - あなたがResponse.Flush();を行うと、これはさえBool IsContentFlushed(string sessionID, string fileName);

ようWebMethodの可能性があなたの店からこのセッションID +ファイル名を削除します。

Response.End()の代わりにResponse.Close()と呼んでください。後者は非常に残酷で、通常はオーバーキルです。

1

ファイルを小さなパケットとして転送し、すべてのパケットが転送されたかどうかを確認して、ダウンロードの状態を追跡できるソリューションがあります。 解決策は私ではありませんが、あなたはここでそれを見つけることができます: File Download in ASP.NET and Tracking the Status of Success/Failure of Download

//Function for File Download in ASP.Net in C# and 
//Tracking the status of success/failure of Download. 
private bool DownloadableProduct_Tracking() 
{ 
//File Path and File Name 
string filePath = Server.MapPath("~/ApplicationData/DownloadableProducts"); 
string _DownloadableProductFileName = "DownloadableProduct_FileName.pdf"; 

System.IO.FileInfo FileName = new System.IO.FileInfo(filePath + "\\" + _DownloadableProductFileName); 
FileStream myFile = new FileStream(filePath + "\\" + _DownloadableProductFileName, FileMode.Open, FileAccess.Read, FileShare.ReadWrite); 

//Reads file as binary values 
BinaryReader _BinaryReader = new BinaryReader(myFile); 

//Ckeck whether user is eligible to download the file 
if (IsEligibleUser()) 
{ 
//Check whether file exists in specified location 
if (FileName.Exists) 
{ 
    try 
    { 
    long startBytes = 0; 
    string lastUpdateTiemStamp = File.GetLastWriteTimeUtc(filePath).ToString("r"); 
    string _EncodedData = HttpUtility.UrlEncode(_DownloadableProductFileName, Encoding.UTF8) + lastUpdateTiemStamp; 

    Response.Clear(); 
    Response.Buffer = false; 
    Response.AddHeader("Accept-Ranges", "bytes"); 
    Response.AppendHeader("ETag", "\"" + _EncodedData + "\""); 
    Response.AppendHeader("Last-Modified", lastUpdateTiemStamp); 
    Response.ContentType = "application/octet-stream"; 
    Response.AddHeader("Content-Disposition", "attachment;filename=" + FileName.Name); 
    Response.AddHeader("Content-Length", (FileName.Length - startBytes).ToString()); 
    Response.AddHeader("Connection", "Keep-Alive"); 
    Response.ContentEncoding = Encoding.UTF8; 

    //Send data 
    _BinaryReader.BaseStream.Seek(startBytes, SeekOrigin.Begin); 

    //Dividing the data in 1024 bytes package 
    int maxCount = (int)Math.Ceiling((FileName.Length - startBytes + 0.0)/1024); 

    //Download in block of 1024 bytes 
    int i; 
    for (i = 0; i < maxCount && Response.IsClientConnected; i++) 
    { 
     Response.BinaryWrite(_BinaryReader.ReadBytes(1024)); 
     Response.Flush(); 
    } 
    //if blocks transfered not equals total number of blocks 
    if (i < maxCount) 
     return false; 
    return true; 
    } 
    catch 
    { 
    return false; 
    } 
    finally 
    { 
    Response.End(); 
    _BinaryReader.Close(); 
    myFile.Close(); 
    } 
} 
else System.Web.UI.ScriptManager.RegisterStartupScript(this, GetType(), 
    "FileNotFoundWarning","alert('File is not available now!')", true); 
} 
else 
{ 
System.Web.UI.ScriptManager.RegisterStartupScript(this, GetType(), 
    "NotEligibleWarning", "alert('Sorry! File is not available for you')", true); 
} 
return false; 
} 
関連する問題