You need to enable JavaScript to run this app.
优惠活动
大模型
产品
解决方案
定价
更多

PuppeteerSharp取消下载问题求助:拦截大文件与阻止IDM弹窗

Hey there, let's work through these two issues you're facing—both are pretty common when dealing with Puppeteer and external download tools, so I've got some practical fixes for you:

1. Fixing IDM Popups Despite --disable-extensions

First, let's clear up why --disable-extensions isn't stopping IDM: IDM doesn't operate as a standard Chrome extension. Instead, it uses system-level hooks (like Browser Helper Objects or shell integrations) to intercept download requests from Chrome. Disabling Chrome's built-in extensions won't block this kind of external tool.

Here are your solutions, ordered by effectiveness:

  • Add the --disable-external-download-manager flag to your Chrome launch args. This forces Chrome to use its own built-in download handler instead of passing requests to external tools like IDM:
    var browser = await Puppeteer.LaunchAsync(new LaunchOptions { 
        Headless = true, 
        ExecutablePath = ChromePath, 
        IgnoreHTTPSErrors = true, 
        Args = new[] { "--disable-extensions", "--disable-external-download-manager" } 
    });
    
  • Double-down on your download behavior configuration to ensure Chrome takes full control. Add allowOverwrite to avoid unnecessary prompts, and confirm the download path is valid:
    await page.Client.SendAsync("Page.setDownloadBehavior", new { 
        behavior = "allow", 
        downloadPath = Environment.GetFolderPath(Environment.SpecialFolder.ApplicationData),
        allowOverwrite = true
    });
    
  • If you still see UI popups, add --disable-features=DownloadBubble to suppress Chrome's native download notification bubble (this is more about cleanup than blocking IDM, but it helps).
  • As a last resort for automated environments, you can temporarily disable IDM's Chrome integration via IDM's settings (though this is not ideal if you need IDM for other tasks).
2. Detecting File Size When Content-Length is Missing

Let's start with a critical mistake in your current code: Content-Length is a response header, not a request header. You're checking the headers sent to the server, but the file size is in the headers sent back from the server. That's why you're not finding it!

Here's how to fix this, depending on the server's response type:

Scenario 1: Server sends a Content-Length header

Switch to listening for the Response event instead of Request, then check the response headers:

await page.SetRequestInterceptionAsync(true);
page.Response += async (s, e) => {
    // First, filter for download requests (adjust this condition to match your use case)
    var isDownload = e.Response.Headers.TryGetValue("Content-Type", out var contentType) && 
                     (contentType.StartsWith("application/") || // Example: zip, pdf, exe
                      contentType.StartsWith("octet-stream"));

    if (isDownload && 
        e.Response.Headers.TryGetValue("Content-Length", out var contentLengthStr) && 
        long.TryParse(contentLengthStr, out var contentLength) && 
        contentLength > GIVENSIZE) {
        // Abort the request to cancel the download
        await e.Request.AbortAsync();
    }
};

Scenario 2: Server uses chunked encoding (no Content-Length)

Some dynamic content (like generated files) uses chunked encoding, so the server doesn't send a Content-Length header. In this case, you'll need to track download progress in real-time and abort once it exceeds your limit. Use Chrome DevTools Protocol (CDP) events for this:

// Enable network tracking to monitor download progress
await page.Client.SendAsync("Network.enable");

// Track data received in real-time
page.Client.MessageReceived += async (s, e) => {
    // Listen for incremental data chunks
    if (e.MessageID == "Network.dataReceived") {
        var data = e.MessageData.ToObject<dynamic>();
        if (data.totalBytes > GIVENSIZE) {
            // Cancel the download immediately
            await page.Client.SendAsync("Network.cancelLoading", new { requestId = data.requestId });
        }
    }
    // Optional: Verify final file size after download completes
    else if (e.MessageID == "Network.loadingFinished") {
        var data = e.MessageData.ToObject<dynamic>();
        if (data.dataLength > GIVENSIZE) {
            // Delete the oversized file from your download path
            var downloadedFile = Path.Combine(Environment.GetFolderPath(Environment.SpecialFolder.ApplicationData), "your-file-name.ext");
            if (File.Exists(downloadedFile)) File.Delete(downloadedFile);
        }
    }
};

// Don't forget to disable network tracking when your task is done
// await page.Client.SendAsync("Network.disable");

(Note: If you don't want to use dynamic, you can create strongly-typed classes to deserialize the CDP message data.)

Bonus Tip

Always filter requests by Content-Type or URL pattern before applying size checks. This prevents you from aborting regular page resources (like images or scripts) by accident.


内容的提问来源于stack exchange,提问作者Inside Man

相关产品推荐
方舟 Agent Plan

超全模态模型 × Harness 升级,最新支持 Deepseek-V4.1-Flash、GLM-5.3 系列、Doubao-Seedream-5.0-pro、Kimi-K3 (部分), 限时 9.9 元起

最近更新时间:2026.05.11 08:06:10