Save PDF from a download link into an object - c#

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!

Related

C# Github Api uploading a pdf file does not work

Im trying to upload a pdf file using the octokit nuget package in c#.
But whenever i try to upload something i have to convert it to a string or i will get an error. If i convert the pdf to a string im able to upload it to my repo, but then its not a valid pdf file.
Im using this code: gitHubClient.Repository.Content.CreateFile(owner, repoName, filepath, new CreateFileRequest($".", File.ReadAllBytes(#"C:/Temp/CUSTOM.pdf").ToString(), branch));
Could someone please help me, i tried researching but i either find old articles or once which only upload a text file.

Convert .h5 content to JSON from an IFormFile

I have an asp net core web application where I can upload files, and on the controller I get the files in IFormFile like this:
public IActionResult Create (AcquisitionDto <IFormFile> acquisitionDto)
Some of these files are images, but one is a .h5 with metadata. And I want to store this information in mongdb, so far so good.
But before writing to mongodb, I want to extract the information inside h5 so I can save it as fields in the mongo document.
In short, I want to open the .h5 file I get in controler as IFormFile and convert its content to JSON.
I am using HDF5DotNet.x64 package to try to open the file, but I can only open .h5 if I pass the file path but cannot open from a byte[] or MemoryStream.
Do you know any way or package to do this?
Thank you very much

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.

how to import and export an excel file using asp.net web api 2.2

I want to do the following I want to be able to upload a file using asp.net web API from a local disk storage to memory and then compress the memory stream and save the memory stream to a db. Also I would like to export the compressed stream back as file name.zip when requested.
I do not know where to start can someone help.
I have tried all searches and tutorials none helped in showing me how to upload to memory stream
ok the following is the the steps I want to do that I need help it I will be grateful if any one can show me
I want to Create an asp.net web api that dose the following
a. Users of the api Will be able to upload and excel file into memory
b. The memory stream that the file has been uploaded into gets zipped in memory.
c. The memory stream is stored into an mssql database.
The user of the api sends a request to the api
a. the file is read back into a memory stream from the db as a zip file
b. the file is then downloaded to the user as a zip file
The above is the break down if any would like to help me i will gladly appreciate it.
Also I would Gladly appreciate it if someone can point me to the right direction links and
videos.
Well after doing small research on the web i found phace one of what i want to do here is the code i hope it helps some one.:
A Basic Controller Accepting Uploads to MemoryStream
public class MyController : ApiController
{
public async Task Post()
{
if (!Request.Content.IsMimeMultipartContent())
throw new HttpResponseException(Request.CreateResponse(HttpStatusCode.NotAcceptable,"This request is not properly formatted"));
var provider = new MultipartMemoryStreamProvider();
await Request.Content.ReadAsMultipartAsync(provider);
foreach (HttpContent ctnt in provider.Contents)
{
//now read individual part into STREAM
var stream = await ctnt.ReadAsStreamAsync();
if (stream.Length != 0)
{
//handle the stream here
}
}
}
I will add snippets of code when i find the rest of the answer to my question.

WebClient.DownloadFile File Corrupt

I'm trying to download files directly from a list of urls.
I was able to download most files successfully except for the .docx. I was able to download the .docx file, but when I try to open it, the error message shows that "The file is corrupt and cannot be opened", when I try to repair it with Microsoft Word, I got another error message saying "Microsoft Office cannot open this file because some parts are missing or invalid". I don't have any issue when download pdf files.
My code is very simple and it looks like this:
WebClient webClient = new WebClient();
webClient.DownloadFile("http://somehost/somefile.docx", "C:\\somefolder\\somefile.docx");
webClient.Dispose(); //I added this line just to see if it will fix the problem but no it didn't
I went to the urls in the browser and make sure that the files does exist and are not corrupted. The urls are just fine and I was able to download files directly from the urls in a browser and the file opens.
Additional Information:
I did find one thing that's different for pdf url and docx url, but I really don't think it has anything to do with my problem. When I go to the pdf url in a browser, the pdf was displayed in the browser. However, when I go to the docx url, the page doesn't show anything, but the download for the file automatically starts. I don't think this will make a difference but just FYI.
EDIT 10:38AM
I just tried the Async method. I was able to download the docx file and open it, but it appear as a blank word document, which is still not correct. The same docx file I download from the browser does have content.
webClient.DownloadFileAsync(new Uri("http://somehost/somefile.docx"),"C:\\somefolder\\somefile.docx");
DownloadFileAsync downloads file in background, and your application probably terminates before download is completed.
You should wait for DownloadFileCompleted event, or use a DownloadFile method that will wait until file will be downloaded.
Thank you everyone for trying to help, I really appreciate it.
I realize that the problem was actually me not concatenating the url correctly. Right, a stupid mistake I made...
WebClient didn't throw error for incorrect format (for whatever reason), and my log file was not logging the actual url that I was trying to connect to, so I didn't realize it was doing the wrong thing.
Anyway, thank you all for the help and the comments that help me figure out what the problem was.

Categories

Resources