Skip to content
New issue

Have a question about this project? Sign up for a free GitHub account to open an issue and contact its maintainers and the community.

By clicking “Sign up for GitHub”, you agree to our terms of service and privacy statement. We’ll occasionally send you account related emails.

Already on GitHub? Sign in to your account

How to download picture like downloading other file types? #660

Closed
ZeroOut opened this issue Jul 11, 2020 · 2 comments
Closed

How to download picture like downloading other file types? #660

ZeroOut opened this issue Jul 11, 2020 · 2 comments

Comments

@ZeroOut
Copy link

ZeroOut commented Jul 11, 2020

something like this:

chromedp.Run(ctx, page.SetDownloadBehavior(page.SetDownloadBehaviorBehaviorAllow).WithDownloadPath(local_path), chromedp.Navigate(url_to_picture))
@Hecatoncheir
Copy link

Something like this:

func (downloader *Downloader) Download(url string) ([]byte, error) {
	tab := NewTabForChrome(downloader.Context)

	done := make(chan bool)

	var requestId network.RequestID

	chromedp.ListenTarget(tab.TabContext, func(ev interface{}) {

		switch ev := ev.(type) {

		case *network.EventRequestWillBeSent:
			req := ev.Request
			if req.URL == url {
				requestId = ev.RequestID
			}

		case *network.EventLoadingFinished:
			if ev.RequestID == requestId {
				close(done)
			}
		}
	})

	err := chromedp.Run(tab.TabContext, chromedp.Tasks{
		page.SetDownloadBehavior(page.SetDownloadBehaviorBehaviorAllow).WithDownloadPath(os.TempDir()),
		chromedp.Navigate(url),
	})
	if err != nil {
		return nil, err
	}

	<-done

	var bytes []byte
	err = chromedp.Run(tab.TabContext, chromedp.ActionFunc(func(cxt context.Context) error {
		bytes, err = network.GetResponseBody(requestId).Do(cxt)
		return err
	}))

	err = tab.Close()
	if err != nil {
		return nil, err
	}

	return bytes, nil
}

@kenshaw
Copy link
Member

kenshaw commented Jan 11, 2021

@Hecatoncheir thanks for the example. Closing this out.

Sign up for free to join this conversation on GitHub. Already have an account? Sign in to comment
Labels
None yet
Projects
None yet
Development

No branches or pull requests

3 participants