Download pdf files from a link - c#

i am working on .Net app that need to download research files from the given link. i checked that most of the published portal show the pdf files on pdf viewer and there to download the file, no .pdf link that can download directly. Can someone recommend how to download files in such cases. example i want to download the pdf from following link https://ietresearch.onlinelibrary.wiley.com/doi/pdf/10.1049/iet-epa.2019.0877 .

As #Philip-Stuyck mentioned in the comments, you can use HttpClient to download the file. Here is the sample code:
using var client = new HttpClient();
var uri = new Uri("https://ietresearch.onlinelibrary.wiley.com/doi/pdf/10.1049/iet-epa.2019.0877");
await using var s = await client.GetStreamAsync(uri);
await using var file = File.Create("iet-epa-localcopy.pdf");
await s.CopyToAsync(file);

Related

Save PDF from a download link into an object

Is there a way I can store a pdf file in an object? I have a JSON response with a download url string to a pdf file that I'll have to somehow download and return. I was able to find online a lot of help, but none seemed to do what I want. I don't want to save a file on the drive, I just want it downloaded into memory and sent as a return of a function.
Let's say something like this:
static Task<PDFObject> getPDF(string url)
{
PDFObject obj = await //download pdf from url
return obj;
}
Is this achievable? If need be, I'm open to incorporating a NuGet package in my project. Thanks in advance guys!

Dropbox upload files to a folder

I am using the REST API (C#) to upload files to Dropbox.
I am able to create a folder in Dropbox using the following API:
https://api.dropboxapi.com/1/fileops/create_folder
and able to upload a file using
https://content.dropboxapi.com/1/files_put/auto/test.jpg
How can I upload files to a particular folder?
Here is my code
var fileurl = string.Format("https://api.dropboxapi.com/1/fileops/create_folder?root=auto&path=test");
var res = await HttpClient.PostAsync(fileurl,null);
HttpClient.DefaultRequestHeaders.TryAddWithoutValidation("Content-Length", backupContent.ContentLength.ToString());
var uploadurl = string.Format("https://content.dropboxapi.com/1/files_put/{0}?root=test", fileName);
await HttpClient.PutAsync(uploadurl , Content);
When using the Dropbox API v1 /files_put endpoint, the path parameter, which is included in the URL, should include the full path where you want to put the file, including any parent folders.
So, to upload a file named "test.txt" into a folder named "test folder", the path would be /test folder/text.txt.
You also don't actually need to explicitly create any new parent folders ahead of time. If a parent folder doesn't already exist, it will automatically be created with the upload.

How to get video file stream from embed url in c#

I tried this below code but video is not playing.
var httpClient = new HttpClient();
var data = await httpClient.GetByteArrayAsync(new Uri("http://www.youtube.com/embed/iRFE-sKOp4I"));
var file = await KnownFolders.MusicLibrary.CreateFileAsync("myfile.mp4", CreationCollisionOption.ReplaceExisting);
var targetStream = await file.OpenAsync(FileAccessMode.ReadWrite);
await targetStream.AsStreamForWrite().WriteAsync(data, 0, data.Length);
await targetStream.FlushAsync();
targetStream.Dispose();
Any one please help me to how to save download..thankyou
I agree with #x..., If you want to download video from Youtube, you need to install the YoutubeExtractor.
But with that same code i can't to download different website videos.
I tested your code with a video download uri "http://video.ch9.ms/ch9/9b56/4f2d0b4d-ea37-4525-8833-128ad6e69b56/uwp01SeriesIntro.mp4", it works fine.
Actually I want to download openload.co/embed/EHmDelqNx94 video is there any possibility..
I just took a look at this video, I find this a web named openload, and this web service share free without limits, but you need to use the develop Api of it. For downloading, you can refer to the download APIs here.
the video url appears after click on play button, is there any way to handle click event by httpclient or any other clients
From the official document of this web, I found this:
get a download link by using download ticket.
You can refer to Download Link.
Update:
i tried openload api, but when get download link it showing captcha error, can you please help me for this
I just reproduced your problem, this is because your source for download is not a public source, it needed authentication. When you get the Download Ticket for download, although the Api said API-Login and API-Key are not required, you need to use the full path https://api.openload.co/1/file/dlticket?file={file}&login={login}&key={key} to get an authenticated ticket, then you can use this ticket to get the download URL.

Download file to Windows Phone with c# and OneDrive API

Someone knows how to make downloading files to phone?
I made a request and received items:
HttpClient httpClient = new HttpClient();
var response = await httpClient.GetAsync(String.Format("https://api.onedrive.com/v1.0/drive/special/approot/children?access_token={0}", token));
var text = await response.Content.ReadAsStringAsync();
I have them id, and now i must do nex request:
https://api.onedrive.com/v1.0/drive/items/{ID}/content
But how to make downloading a file in the local storage application? (file format is .txt)
Once you've made the request to download the content, you should receive a HTTP 302 response. To download the file, you'll need to follow the URL provided in the Location header of the response. You can then use the FileSavePicker class to save the file locally. More info on the FileSavePicker can be found at https://msdn.microsoft.com/en-us/library/windows/apps/windows.storage.pickers.filesavepicker.aspx . I hope that helps.

How to download file from http server that requires a referral before allowing the download?

I usually download file using following code:
My.Computer.Network.DownloadFile("url of the file","filepath to save the file")
But recently I encountered a site that only allow file download if you click it from its site and not via direct downloading from vb.net code.
My question is: How to download file from http server that requires a referral before allowing the download?
Try adding the Referer header manually:
WebClient wc = new WebClient();
wc.Headers.Add("Referer","http://whatever.com");
wc.DownloadFile("url of the file","filepath to save the file");

Categories

Resources