3

私はこのダウンロード機能を持っています、そしてそれはうまく働いています。ただし、ファイルサイズが1.35 gbのファイルの場合、ダウンロードは300 Mb、382、400 mb、または1.27Gbで停止します。私は何が間違っているのですか?(ダウンロード機能は、ファイルを非表示にする必要があり、Webサイトに公開されていない可能性があるため、このように作成されています。)

public static void downloadFunction(string filename)
{
    string filepath = @"D:\texts\New folder\DLfolder\" + filename;
    string contentType = "application/x-newton-compatible-pkg";

    Stream iStream = null;
    // Buffer to read 10K bytes in chunk
    //byte[] buffer = new Byte[10000];
    // Buffer to read 1024K bytes in chunk

    byte[] buffer = new Byte[1048576];

    // Length of the file:
    int length;

    // Total bytes to read:
    long dataToRead;

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

        // Total bytes to read:
        dataToRead = iStream.Length;
        HttpContext.Current.Response.ContentType = contentType;
        HttpContext.Current.Response.AddHeader("Content-Disposition", "attachment; filename=" + HttpUtility.UrlEncode(filename, System.Text.Encoding.UTF8));
        HttpContext.Current.Response.AddHeader("Content-Length", iStream.Length.ToString());

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

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

                // Flush the data to the HTML output.
                HttpContext.Current.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.
        HttpContext.Current.Response.Write("Error : " + ex.Message + "<br />");

        HttpContext.Current.Response.ContentType = "text/html";
        HttpContext.Current.Response.Write("Error : file not found");
    }
    finally
    {
        if (iStream != null)
        {
            //Close the file.
            iStream.Close();
        }
        HttpContext.Current.Response.End();
        HttpContext.Current.Response.Close();
    }
} 
4

1 に答える 1

0

リクエストのタイムアウトに達した可能性があります。60 秒や 300 秒など、所定の時間が経過した後にファイルのダウンロードが停止した場合は、注意してください。その場合は、アプリケーションの web.config でタイムアウトを構成できます。

于 2013-03-13T11:53:02.653 に答える