1

アイデアは単純です。ユーザーが別のWebサイトでホストされているファイルの直接リンクを配置できるサービスを作成しています。プログラムは、そのリモートサーバーへのストリームを開き、ファイルの読み取りをバイト単位で開始して、読み取った各ファイルを返します。ユーザーへのバイト。

例:インターネットダウンロードマネージャーが私のページに移動し、コードがリモートファイルをフェッチしてバイト単位で読み取り、各バイトをインターネットダウンロードマネージャーに返してファイルをダウンロードします。

これが私のコードです

    public void Index()
    {
        using (WebClient wcDownload = new WebClient())
        {
            try
            {
                // Create a request to the file we are downloading
                HttpWebRequest webRequest = (HttpWebRequest)WebRequest.Create("http://FILE-IS-NOT-ON-MY-SERVER.com/file.zip");
                // Set default authentication for retrieving the file
                webRequest.Credentials = CredentialCache.DefaultCredentials;
                // Retrieve the response from the server
                HttpWebResponse webResponse = (HttpWebResponse)webRequest.GetResponse();
                // Ask the server for the file size and store it
                Int64 fileSize = webResponse.ContentLength;

                // Open the URL for download 
                Stream strResponse = wcDownload.OpenRead("http://FILE-IS-NOT-ON-MY-SERVER.com/file.zip");

                // It will store the current number of bytes we retrieved from the server
                int bytesSize = 0;
                // A buffer for storing and writing the data retrieved from the server
                byte[] downBuffer = new byte[500000000];


                // Loop through the buffer until the buffer is empty
                while ((bytesSize = strResponse.Read(downBuffer, 0, downBuffer.Length)) > 0)
                {
                    // i want to return each byte to the user for example Internet Download Manager
                }


                // When the above code has ended, close the streams
                strResponse.Close();
            }
            catch (Exception)
            {

            }
        }
    }

私はそれが厄介であることを知っていますが、私は本当に各バイトをユーザーに返す方法を知りません。

4

2 に答える 2

1

このリンクhttp://support.microsoft.com/kb/812406はあなたに大いに役立つと思います!必要なのは、ダウンロード用に開いているファイルを変更することだけです。

于 2013-03-24T10:26:02.733 に答える
1

つまり、要求された応答からストリームを取得し、そのストリームを応答ストリームに割り当てます(または応答ストリームに書き込みます)。

このSOの質問には良い答えがあると思います。

于 2013-03-25T05:04:36.073 に答える