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

Need API for downloading a file #302

Closed
studylamp opened this issue Jun 24, 2020 · 9 comments
Closed

Need API for downloading a file #302

studylamp opened this issue Jun 24, 2020 · 9 comments
Assignees
Labels
feature request feature request

Comments

@studylamp
Copy link

studylamp commented Jun 24, 2020

As far as I can tell, there is no way to download the contents of a URL to a file or memory stream using the browser's current session/cookies/etc.

Especially since we current cannot access the browser's cookies #4 , we need to be able to download non-viewable content or attachments into our own temporary file location and provide a viewer outside of the browser. When the content is behind cookies or other authorization, we have no way to do this through the standard Windows http apis.

Ideally, we would have an API for requesting the contents of a URL, which the browser would handle by responding with cookies, handling authorization, and redirects -- with a callback for the data as it is received, or at least something notifying when the download is complete.

AB#28992572

@liminzhu
Copy link
Member

@studylamp thanks for opening the issue. When you say access the browser cookies, do you mean webview app access the browser (Edge) cookies, or just access webview cookies? The reason I'm asking is we made a conscious decision to separate browser and webview data for obvious security reason of not letting apps steal user data/credentials they entered in the browser.

If we are talking about this in the context of webview, we can consider a http request API from webview, but I also want to discuss a couple alternatives and see whether they works for you.

  • If you want to get things through HTTP request, consider injecting a script to use XHR from JavaScript, which will be sent from user's signed-in web session.
  • If you want to use native HTTP request APIs and need to get signed in, you can still grab the cookies/auth info from webview. We're actively working on cookie management APIs and a WebResponseReceived API which you can use to grab the auth headers. Before that happens, you can use a workaround from invoking CDP APIs. There are CDP methods, such as Network.getCookies for grabbing cookies, and the Network.requestWillBeSentExtraInfo for grabbing the auth headers.

Btw, how are you detecting non-viewable content? Do you as the developer also own the web content, or you're detecting things through webResourceRequested?

@studylamp
Copy link
Author

studylamp commented Jun 24, 2020

When you say access the browser cookies, do you mean webview app access the browser (Edge) cookies, or just access webview cookies?

I mean the WebView2 control's cookies from the current session as it exists inside of my application. Same issue as #4 .

we can consider a http request API from webview

This would be very very helpful.

how are you detecting non-viewable content?

Well, sometimes we need this for even viewable content (example: download a plain text file to be processed in our application, not shown in the web view), but if the mime type is "attachment" I want to be able to control the download process rather than shove the bits into the user's downloads folder. We know the URL before-hand and just want to be able to grab it through http using the cookies/etc after the user has already logged in. A simple API for http request allowing me to direct the content to a memory stream or file of my choice would be a good solution. In this particular case, a handler for begin/end download would even be enough.

My current workaround is to just use a web resource filter and keep track of the current cookies being sent in the requests, and then form a simple http request using them, but outside of the webview.

@liminzhu liminzhu added the feature request feature request label Jun 24, 2020
@liminzhu
Copy link
Member

Thanks for the details. Sounds like you're on a working but painful workaround. We will track this as a feature request on our backlog.

@ukandrewc
Copy link

You can use XMLHTTPRequest for this e.g:

let r = new XMLHttpRequest()
r.open("GET", "myurl", false)
r.send()
return r.responseText

@studylamp
Copy link
Author

XMLHttpRequest

A good suggestion and it is fine for one of our use cases. However, we also need to be able to download very large video files after a purchase in the web view. Managing that in javascript and hoping it is not interrupted by new navigation in the web view is not ideal. An async http get API would be a robust solution and is supported by other browser controls I have worked with, so hopefully this feature request will be implemented.

@ukandrewc
Copy link

Ah yes, I see. If MS don't implement anything, I will be adding it to this control: https://github.com/ukandrewc/Webview2.Winforms.DOM

@ukandrewc
Copy link

ukandrewc commented Jun 26, 2020

Updated the control to get cookies and translate them into .Net CookieCollection
https://github.com/ukandrewc/Webview2.Winforms.DOM

Dim Urls As New WVUrlArray({"https://google.co.uk", "https://.google.com"})
Dim Cookies = WebView2.Protocol.GetCookies(Urls).ToCookieCollection()

@champnic champnic added feature request feature request and removed feature request feature request labels Sep 2, 2020
@liminzhu
Copy link
Member

After some internal consideration, we are not planning on working on an http request method in the near future. HTTP requests are generally not considered in scope for WebView. Developer can either use XHR or native HTTP method + use cookies from webview to authenticate the native http libraries.

We can still discuss this with additional input. Feel free to add comment or ask for re-opening in the future.

@darbid
Copy link

darbid commented Feb 15, 2021

I am here as I missed this feature while looking at WebView2. I have used CEFSharp and a .NET HTTPClient to download files. I am not an expert but it took me a while to work out the Headers needed especially cookies needed. I got it working eventually. Then the website I was targeting changed cookies and headers. The HTTPClient now does not work.

CefSharp exposes which means CEF exposes a download feature which uses the credentials of the current user logged in. Chrome/Edge Extensions also expose the ability to download something as well.

Objectively you could say that a number of different developers think it is a good feature. All of the above would also have the ability to use XHR and native HTTP (extensions could send the cookies to a native host which could then use HTTP).

Correct me if I am wrong but you are using CEF so the feature is there to be used, they thought it important enough to expose it, you simply choose not to hook it up?

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

No branches or pull requests

5 participants