0

コントローラーからクライアントに大きなファイル (200-800MB) を提供する必要があります。FileStreamResult をテストしましたが、このクラスはファイル全体をメモリにバッファリングしました。この動作は、私のプロジェクトには十分ではありません。

さらに、ここからアプローチをテストします: http://support.microsoft.com/kb/812406。メモリに関しては、これはかなり良いように見えますが、ファイルはクライアントに完全にダウンロードされていません (元のファイルは 210222 KB で、ダウンロードされたファイルは 209551 から 209776 です)。これは、約 0.5 MB が失われていることを意味します (その結果、ファイルが破損します)。

誰かアイデアはありますか?とにかくこれを行う最良の方法は何ですか?すべてに感謝しています。

将来のユーザーのために、リンクは次のコードを指していました。

System.IO.Stream iStream = null;

// Buffer to read 10K bytes in chunk:
byte[] buffer = new Byte[10000];

// Length of the file:
int length;

// Total bytes to read:
long dataToRead;

// Identify the file to download including its path.
string filepath  = "DownloadFileName";

// Identify the file name.
string  filename  = System.IO.Path.GetFileName(filepath);

try
{
    // Open the file.
    iStream = new System.IO.FileStream(filepath, System.IO.FileMode.Open, 
                System.IO.FileAccess.Read,System.IO.FileShare.Read);


    // Total bytes to read:
    dataToRead = iStream.Length;

    Response.ContentType = "application/octet-stream";
    Response.AddHeader("Content-Disposition", "attachment; filename=" + filename);

    // Read the bytes.
    while (dataToRead > 0)
    {
        // Verify that the client is connected.
        if (Response.IsClientConnected) 
        {
            // Read the data in buffer.
            length = iStream.Read(buffer, 0, 10000);

            // Write the data to the current output stream.
            Response.OutputStream.Write(buffer, 0, length);

            // Flush the data to the HTML output.
            Response.Flush();

            buffer= new Byte[10000];
            dataToRead = dataToRead - length;
        }
        else
        {
            //prevent infinite loop if user disconnects
            dataToRead = -1;
        }
    }
}
catch (Exception ex) 
{
    // Trap the error, if any.
    Response.Write("Error : " + ex.Message);
}
finally
{
    if (iStream != null) 
    {
        //Close the file.
        iStream.Close();
    }
    Response.Close();
}

アップデート

これは私の行動です:

    public DownloadResult TransferTest()
    {
        string fullFilePath = @"C:\ws\Test\Test\Templates\example.pdf";
        return new DownloadResult(fullFilePath);
    }

ブラウザからアクションを直接呼び出すだけです ( http://xxx.xxx/Other/TransferTest )。

4

1 に答える 1

3

コードは基本的に健全に見えます - からの戻り値を多かれ少なかれ正しく処理していますRead(私がうるさいなら、 をチェックすると言い<=0ますが、おそらくファイルにロックがあるため、これは予期される動作ではありません)。

発生する唯一のことは次のとおりです。

Response.OutputStream.Flush();

そしておそらく:

Response.OutputStream.Close();

出力ストリームが確実にフラッシュされるようにします。

于 2014-04-17T14:57:06.673 に答える