How to use URLDownloadToFile in CodeActivity in UiPath

Hi Team,

I want to use URLDownloadToFile to download file directly. I know End point URL and Destination file path as well.

can you please guide me to utilize URLDownloadToFile functionality in UiPath with code activity.

Regards,
Vijay.

@Vijay_Kumar_C
I’m not sure about URLDownloadToFile but below activity, you can use in that case…
Passing your download URL to open browser inside do…

https://docs.uipath.com/activities/docs/get-last-downloaded-file

To access URLDownloadToFile you will need to use P\Invoke which is not supported by UiPath as far I know. pinvoke.net: URLDownloadToFile (urlmon)

Have you tested WebClient.DownloadFile() instead?

Using web As New System.Net.WebClient
	web.DownloadFile("https://www.google.com/images/branding/googlelogo/1x/googlelogo_color_272x92dp.png", "C:\Temp\logo.png")
End Using
1 Like

Hey @Pravin_Patil1 ,

Thank you for reply.

This activity is to check file successfully downloaded or not. But i am trying to download without clicking on Save or Save As button.

Regards,
Vijay.

How can we use this inside UiPath Studio ??

Regards,
Vijay.

Use it with Invoke Code:

image

i am getting following error.

Change the language to VB.NET.

image

Yes, I suggested you to change it to VB.NET.

image

Yeah i did. syntax is correct . now file is getting corrupted.

will it support for any kind of file format? or any limitation with some other extension files (.zip) ?

Regards,
Vijay.

It should be able to handle any type of files. Is it a big file you are trying to download? It might be that the “using” statement is disposing of the web client before the download is done. You can change the code to below instead and let the garbage collector handle the disposing instead.

Dim web As New System.Net.WebClient
web.DownloadFile("https://www.google.com/images/branding/googlelogo/1x/googlelogo_color_272x92dp.png", "C:\Temp\logo.png")

Hey @ptrobot - can you please tell me the differences between URLDownloadToFile and System.Net.WebClient.

Regards,
Vijay.

UrDownloadToFile belongs to the Win32 API library and WebClient.DownloadFile belongs to the .NET libray. They are doing basically the same thing.

Hey @ptrobot

Now i am getting 401 error with Webclient but UrDownloadToFile with macro is working.

Any alternate way that we can use UrDownloadToFile to download file ??

Regards,
Vijay.

Sorry to hear that. 401 is Unauthorized error. Does the web site your are downloading from requires any credentials? URLDownloadToFile requires Internet Explorer so it might get the credentials from there. WebClient.DownloadFile, on the other hand, doesn’t require Internet Explorer so it might explain why it fails.

I haven’t managed to find any way to use P/Invoke in UiPath so to be able to use URLDownloadToFile you will need to do it via other scripts. For example, you could write a Powershell script that calls URLDownloadToFile and then in UiPath you can call that PS script.

If you already have the code in a macro, why don’t you call the macro from UiPath instead?